Vic*_*Vic 3 regression r statistics-bootstrap
我想使用新lme4包的新bootMer()功能(当前的开发人员版本).我是R的新手,不知道我应该为其FUN参数编写哪个函数.它说它需要一个数字向量,但我不知道该函数将执行什么.所以我有一个混合模型公式,它被转换为bootMer(),并且有许多重复.所以我不知道那个外部函数是做什么的?它应该是引导方法的模板吗?他的bootMer中是否已经实现了自举方法?那么为什么他们需要外部的"利益统计"呢?我应该使用哪种统计数据?
以下语法是否适用?R继续产生错误,产生FUN必须是数字向量.我不知道如何将估计与"适合"分开,甚至我应该首先做到这一点?我可以说我迷失了那种"有趣"的说法.另外我不知道我应该使用变量"Mixed5"传递混合模型glmer()公式还是应该传递一些指针和引用?我在示例中看到X(bootMer()的第一个参数是*lmer()对象.我想编写*Mixed5但是它呈现错误.
非常感谢.
我的代码是:
library(lme4)
library(boot)
(mixed5 <- glmer(DV ~ (Demo1 +Demo2 +Demo3 +Demo4 +Trt)^2
+ (1 | PatientID) + (0 + Trt | PatientID)
, family=binomial(logit), MixedModelData4))
FUN <- function(formula) {
fit <- glmer(DV ~ (Demo1 +Demo2 +Demo3 +Demo4 +Trt)^2
+ (1 | PatientID) + (0 + Trt | PatientID)
, family=binomial(logit), MixedModelData4)
return(coef(fit))
}
result <- bootMer(mixed5, FUN, nsim = 3, seed = NULL, use.u = FALSE,
type = c("parametric"),
verbose = T, .progress = "none", PBargs = list())
result
FUN
fit
Run Code Online (Sandbox Code Playgroud)
而错误:
Error in bootMer(mixed5, FUN, nsim = 3, seed = NULL, use.u = FALSE, type = c("parametric"), :
bootMer currently only handles functions that return numeric vectors
Run Code Online (Sandbox Code Playgroud)
-------------------------------------------------- ------更新------------------------------------------- ----------
我像Ben指示的那样编辑了代码.代码运行得很好,但SE和偏差都是零.你也知道如何从这个输出中提取P值(对我来说很奇怪)?我应该使用afex包的mixed()吗?
我修改过的代码:
library(lme4)
library(boot)
(mixed5 <- glmer(DV ~ (Demo1 +Demo2 +Demo3 +Demo4 +Trt)^2
+ (0 + Trt | PatientID)
, family=binomial(logit), MixedModelData4))
FUN <- function(fit) {
fit <- glmer(DV ~ (Demo1 +Demo2 +Demo3 +Demo4 +Trt)^2
+ (1 | PatientID) + (0 + Trt | PatientID)
, family=binomial(logit), MixedModelData4)
return(fixef(fit))
}
result <- bootMer(mixed5, FUN, nsim = 3)
result
Run Code Online (Sandbox Code Playgroud)
-------------------------------------------------- ------更新2 ------------------------------------------ -----------
我也试过以下但是代码生成警告并且没有给出任何结果.
(mixed5 <- glmer(DV ~ Demo1 +Demo2 +Demo3 +Demo4 +Trt
+ (1 | PatientID) + (0 + Trt | PatientID)
, family=binomial(logit), MixedModelData4))
FUN <- function(mixed5) {
return(fixef(mixed5))}
result <- bootMer(mixed5, FUN, nsim = 2)
Run Code Online (Sandbox Code Playgroud)
警告信息:
In bootMer(mixed5, FUN, nsim = 2) : some bootstrap runs failed (2/2)
> result
Call:
bootMer(x = mixed5, FUN = FUN, nsim = 2)
Bootstrap Statistics :
WARNING: All values of t1* are NA
WARNING: All values of t2* are NA
WARNING: All values of t3* are NA
WARNING: All values of t4* are NA
WARNING: All values of t5* are NA
WARNING: All values of t6* are NA
Run Code Online (Sandbox Code Playgroud)
-------------------------------------------------- ------更新3 ------------------------------------------ -----------
此代码以及生成的警告:
FUN <- function(fit) {
return(fixef(fit))}
result <- bootMer(mixed5, FUN, nsim = 2)
Run Code Online (Sandbox Code Playgroud)
警告和结果:
Warning message:
In bootMer(mixed5, FUN, nsim = 2) : some bootstrap runs failed (2/2)
> result
Call:
bootMer(x = mixed5, FUN = FUN, nsim = 2)
Bootstrap Statistics :
WARNING: All values of t1* are NA
WARNING: All values of t2* are NA
WARNING: All values of t3* are NA
WARNING: All values of t4* are NA
WARNING: All values of t5* are NA
WARNING: All values of t6* are NA
Run Code Online (Sandbox Code Playgroud)
Ben*_*ker 10
这里基本上有两个(简单)混淆.
coef()(返回矩阵列表)和fixef()(返回固定效果系数的向量):我认为这fixef()是你想要的,尽管你可能想要类似的东西c(fixef(mixed),unlist(VarCorr(mixed))).FUN应该将拟合的模型对象作为输入...例如:
library(lme4)
library(boot)
mixed <- glmer(incidence/size ~ period + (1|herd),
weights=size, data=cbpp, family=binomial)
FUN <- function(fit) {
return(fixef(fit))
}
result <- bootMer(mixed, FUN, nsim = 3)
result
## Call:
## bootMer(x = mixed, FUN = FUN, nsim = 3)
## Bootstrap Statistics :
## original bias std. error
## t1* -1.398343 -0.20084060 0.09157886
## t2* -0.991925 0.02597136 0.18432336
## t3* -1.128216 -0.03456143 0.05967291
## t4* -1.579745 -0.08249495 0.38272580
##
Run Code Online (Sandbox Code Playgroud)