Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用R获得置信区间的bootstrap_R_Statistics Bootstrap - Fatal编程技术网

使用R获得置信区间的bootstrap

使用R获得置信区间的bootstrap,r,statistics-bootstrap,R,Statistics Bootstrap,我试图用非参数引导法来引导可靠性估计 我已经编写了下面的代码,其中创建了一个模型,然后引导1000次,以获得两个可靠性统计信息Alpha和Omega 我能够以置信区间获得第一个构造的Alpha和ω:visual=~x1+x2+x3,但对于其他构造text和speed 当我运行boot函数时,我会看到所有这些函数的结果 # bootstrapping with 1000 replications results <- boot(data=data, statistic=reliability

我试图用非参数引导法来引导可靠性估计 我已经编写了下面的代码,其中创建了一个模型,然后引导1000次,以获得两个可靠性统计信息Alpha和Omega 我能够以置信区间获得第一个构造的Alpha和ω:
visual=~x1+x2+x3
,但对于其他构造
text
speed
当我运行boot函数时,我会看到所有这些函数的结果

# bootstrapping with 1000 replications
results <- boot(data=data, statistic=reliability, R=500, formula=HS.model,parallel = 'snow')

> results$t0

        visual   textual     speed     total
alpha  0.6261171 0.8827069 0.6884550 0.7604886
omega  0.6253180 0.8851754 0.6877600 0.8453351
omega2 0.6253180 0.8851754 0.6877600 0.8453351
omega3 0.6120052 0.8850608 0.6858417 0.8596204
avevar 0.3705589 0.7210163 0.4244883 0.5145874
#使用1000个复制进行引导
结果$t0
视觉文本总速度
阿尔法0.6261171 0.8827069 0.6884550 0.7604886
欧米茄0.6253180 0.8851754 0.6877600 0.8453351
欧米茄2 0.6253180 0.8851754 0.6877600 0.8453351
omega3 0.6120052 0.8850608 0.6858417 0.8596204
avevar 0.3705589 0.7210163 0.4244883 0.5145874
下面是我公认的拙劣尝试。有人能帮忙吗

library(lavaan)
library(semTools)
library(boot)

data <- HolzingerSwineford1939

HS.model <- 'visual  =~ x1 + x2 + x3
textual =~ x4 + x5 + x6
speed   =~ x7 + x8 + x9 '

# function to reliability stats
reliability <- function(formula, data, indices) {
  data = data
  d <- data[indices,] # allows boot to select sample
  fit <- cfa(HS.model, data=d)
  semTools::reliability(fit)
}

# bootstrapping with 500 replications
results <- boot(data=data, statistic=reliability, R=500, formula=HS.model,parallel = 'snow')

# Get the confidence intervals
conf_interval_alpha <- boot.ci(results, type="bca", index = 1)

# Retrieve the Alpha and confidence intervals
alpha <- conf_interval_alpha$t0
alpha.ci <- conf_interval_alpha$bca[,c(4,5)]

# Retrieve the Omega and confidence intervals  
conf_interval_omega <- boot.ci(results, type="bca", index = 2)
omega <- conf_interval_omega$t0
omega.ci <- conf_interval_omega$bca[,c(4,5)]
库(拉万)
图书馆(semTools)
库(启动)

数据您首先需要查看完整数据集的
可靠性
返回的内容:

> reliability(data=data)
          visual   textual     speed     total
alpha  0.6261171 0.8827069 0.6884550 0.7604886
omega  0.6253180 0.8851754 0.6877600 0.8453351
omega2 0.6253180 0.8851754 0.6877600 0.8453351
omega3 0.6120052 0.8850608 0.6858417 0.8596204
avevar 0.3705589 0.7210163 0.4244883 0.5145874
然后您需要查看从
引导返回的内容
-调用:

> str(results)
List of 11
 $ t0       : num [1:5, 1:4] 0.626 0.625 0.625 0.612 0.371 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:5] "alpha" "omega" "omega2" "omega3" ...
  .. ..$ : chr [1:4] "visual" "textual" "speed" "total"
 $ t        : num [1:500, 1:20] 0.594 0.607 0.613 0.669 0.621 ...
 $ R        : num 500
 $ data     :'data.frame':  301 obs. of  15 variables:
  ..$ id    : int [1:301] 1 2 3 4 5 6 7 8 9 11 ...
  ..$ sex   : int [1:301] 1 2 2 1 2 2 1 2 2 2 ...
  ..$ ageyr : int [1:301] 13 13 13 13 12 14 12 12 13 12 ...
  ..$ agemo : int [1:301] 1 7 1 2 2 1 1 2 0 5 ...
  ..$ school: Factor w/ 2 levels "Grant-White",..: 2 2 2 2 2 2 2 2 2 2 ...
  ..$ grade : int [1:301] 7 7 7 7 7 7 7 7 7 7 ...
  ..$ x1    : num [1:301] 3.33 5.33 4.5 5.33 4.83 ...
  ..$ x2    : num [1:301] 7.75 5.25 5.25 7.75 4.75 5 6 6.25 5.75 5.25 ...
  ..$ x3    : num [1:301] 0.375 2.125 1.875 3 0.875 ...
  ..$ x4    : num [1:301] 2.33 1.67 1 2.67 2.67 ...
  ..$ x5    : num [1:301] 5.75 3 1.75 4.5 4 3 6 4.25 5.75 5 ...
  ..$ x6    : num [1:301] 1.286 1.286 0.429 2.429 2.571 ...
  ..$ x7    : num [1:301] 3.39 3.78 3.26 3 3.7 ...
  ..$ x8    : num [1:301] 5.75 6.25 3.9 5.3 6.3 6.65 6.2 5.15 4.65 4.55 ...
  ..$ x9    : num [1:301] 6.36 7.92 4.42 4.86 5.92 ...
 $ seed     : int [1:626] 403 330 1346657232 1136157038 -874329217 857221657 1850455833 952027245 2020402269 -1198488986 ...
 $ statistic:function (formula, data, indices)  
  ..- attr(*, "srcref")=Class 'srcref'  atomic [1:8] 1 16 6 1 16 1 1 6
  .. .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x7fa57918d430> 
 $ sim      : chr "ordinary"
 $ call     : language boot(data = data, statistic = reliability, R = 500, formula = HS.model, parallel = "snow")
 $ stype    : chr "i"
 $ strata   : num [1:301] 1 1 1 1 1 1 1 1 1 1 ...
 $ weights  : num [1:301] 0.00332 0.00332 0.00332 0.00332 0.00332 ...
 - attr(*, "class")= chr "boot"
str(结果) 11人名单 $t0:num[1:5,1:4]0.6260.6250.6250.6120.371。。。 ..-attr(*,“dimnames”)=2个列表 .. ..$ : chr[1:5]“α”“ω”“ω2”“ω3”。。。 .. ..$ : chr[1:4]“视觉”“文字”“速度”“总计” $t:num[1:500,1:20]0.5940.6070.6130.6690.621。。。 $R:num 500 $data:'data.frame':301 obs。在15个变量中: ..$id:int[1:301]12345678911。。。 ..$sex:int[1:301]12 12。。。 ..$ageyr:int[1:301]13 13 12 12 12。。。 ..$agemo:int[1:301]1 7 1 2 1 2 0 5。。。 ..$school:Factor w/2级“Grant White”..:2。。。 ..$grade:int[1:301]7。。。 ..$x1:num[1:301]3.335.334.554.334.83。。。 ..$x2:num[1:301]7.75 5.25 5.25 7.75 4.75 5 6.25 5.75 5.25。。。 ..$x3:num[1:301]0.375 2.125 1.875 3 0.875。。。 ..$x4:num[1:301]2.331.671 2.672.67。。。 ..$x5:num[1:301]5.75 3 1.75 4.5 4 3 6 4.25 5.75 5。。。 ..$x6:num[1:301]1.2861.2860.4292.4292.571。。。 ..$x7:num[1:301]3.393.783.263.7。。。 ..$x8:num[1:301]5.756.253.955.366.366.656.255.154.654.55。。。 ..$x9:num[1:301]6.367.924.424.865.92。。。 $seed:int[1:626]403330 1346657232 1136157038-874329217 857221657 1850455833 952027245 2020402269-1198488986。。。 $statistic:函数(公式、数据、索引) ..-attr(*,“srcref”)=类“srcref”原子[1:8]116 .. .. ..- attr(*,“srcfile”)=类“srcfilecopy”、“srcfile” $sim:chr“普通” $call:语言启动(数据=数据,统计数据=可靠性,R=500,公式=HS.model,parallel=“snow”) $stype:chr“i” $strata:num[1:301]1。。。 $weights:num[1:301]0.00332 0.00332 0.00332 0.00332 0.00332 0.00332。。。 -属性(*,“类”)=chr“启动”

。。。。因此,
results$t0
包含所有三个模型参数估计。

基本调试。。。在刷新会话中执行此操作…并读取错误消息。我遇到的第一个错误是:ERsum中的错误(beta[I],tau.found.sym.optim,m+1,m+n):dims[product 666]与对象的长度不匹配[999]此外:警告消息:在y-X%*%beta中:较长的对象长度不是较短对象长度的倍数抱歉,是的,我现在看到了。我会马上更新代码hi@42-,谢谢你的回复。我能够提取
omega
alpha
点统计数据,但我还想获得在运行代码
boot.ci(results,type=“bca”,index=1)
时无法看到的置信区间
visual
text
speed
阅读
?boot.ci
的帮助页面,并特别注意“index”参数。使用默认值,您只能获得第一个参数的CI估计值。