【问题标题】:how do you create a function that calculates confidence intervals given the value of 1 - a (alpha)?给定 1 - a (alpha) 的值,如何创建一个计算置信区间的函数?
【发布时间】:2022-01-05 22:38:54
【问题描述】:

我对 r 还很陌生,所以我提前道歉。

我编码的主要问题是我只知道如何为给定 95% 置信区间的函数编码,所以我想知道是否有一种方法可以在“level=”中输入一个数字,即不是 95% 并且仍然得到一个置信区间。

这是我的代码:

blah <- function(x1,x2,level=0.95){

  n1 <- length(x1)

  n2 <- length(x2)

  s1 <- sd(x1)


  s2 <- sd(x2)

  CI <- (s1^2 / s2^2) / qf(c(.975,.025), df1 = n1-1, df2 = n2-1)

  return(list("95% CI"=CI))

}

【问题讨论】:

    标签: r function confidence-interval


    【解决方案1】:

    你可以试试,

    library(scales)
    
    blah <- function(x1,x2,level=0.95){
      
      n1 <- length(x1)
      
      n2 <- length(x2)
      
      s1 <- sd(x1)
      
      
      s2 <- sd(x2)
      
      CI <- (s1^2 / s2^2) / qf(c((1+level)/2,(1-level)/2), df1 = n1-1, df2 = n2-1)
      
      
      text <- paste0(scales::percent(level), " CI")
      return(setNames(list(CI), text))
      
      
    }
    
    blah(c(1:10), c(11:20), .95)
    $`95% CI`
    [1] 0.2483859 4.0259942
    
    blah(c(1:10), c(11:20), .90)
    $`90% CI`
    [1] 0.3145749 3.1788931
    

    【讨论】:

      猜你喜欢
      • 2017-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-16
      • 1970-01-01
      • 1970-01-01
      • 2021-12-07
      • 1970-01-01
      相关资源
      最近更新 更多