我对R使用的功能有疑问。这是一个我想用来引导我的集群数据的函数。我想在每个bootstrap复制上使用我的gee模型。当我使用我的函数时,我得到一个错误,说该对象" id"找不到。我认为这可能与使用全球和本地环境有关。
我的数据具有以下结构:
Outcome Time Treatment Cluster ID
500 1 1 Carl 1
800 2 1 Carl 1
1000 3 1 Carl 1
1200 1 2 Pete 2
400 2 2 Pete 2
550 3 2 Pete 2
300 1 1 Rose 3
我的语法如下:
clusbootreg <- function(formula,family,data,id, waves,corstr,cluster, reps=4){
reg1 <- geeglm(formula,family,data,id,waves,corstr)
clusters <- names(table(cluster))
sterrs <- matrix(NA, nrow=reps, ncol=length(coef(reg1)))
for(i in 1:reps){
index <- sample(1:length(clusters), length(clusters), replace=TRUE)
aa <- clusters[index]
bb <- table(aa)
bootdat <- NULL
for(j in 1:max(bb)){
cc <- data[cluster %in% names(bb[bb %in% j]),]
for(k in 1:j){
bootdat <- rbind(bootdat, cc)
}
}
sterrs[i,] <- coef(geeglm(formula,family,bootdat,id,waves,corstr))
}
val <- cbind(coef(reg1),apply(sterrs,2,sd))
colnames(val) <- c("Estimate","Std. Error")
return(val)
}
clusbootreg(formula=Outcome~Treatment+Time+Time*Treatment,family=Gamma(link = "log"),data=data,id=ID,waves=Time, cluster=data$Cluster, reps=4)
出现以下错误消息:
Error in eval(expr, envir, enclos) : object 'id' not found
有谁知道如何解决这个问题?我现在被困了两天。
Traceback告诉我以下
11: eval(expr, envir, enclos)
10: eval(extras, data, env)
9: model.frame.default(formula = formula, data = data, subset = waves,
weights = id, na.action = corstr, drop.unused.levels = TRUE)
8: stats::model.frame(formula = formula, data = data, subset = waves,
weights = id, na.action = corstr, drop.unused.levels = TRUE)
7: eval(expr, envir, enclos)
6: eval(mf, parent.frame())
5: glm(formula = formula, family = family, data = data, weights = id,
subset = waves, na.action = corstr)
4: eval(expr, envir, enclos)
3: eval(glmcall, parent.frame())
2: geeglm(formula, family, data, id, waves, corstr) at #2
答案 0 :(得分:0)
您应该研究位置参数匹配和命名参数之间的区别。在我纠正所有错误之后,错误仍然存在。
问题是您在函数体外部创建了公式,这导致它与全局环境相关联。你需要解决这个问题:
testDF <- read.table(text = "Outcome Time Treatment Cluster ID
500 1 1 Carl 1
800 2 1 Carl 1
1000 3 1 Carl 1
1200 1 2 Pete 2
400 2 2 Pete 2
550 3 2 Pete 2
300 1 1 Rose 3", header = TRUE)
library(geepack)
clusbootreg <- function(formula,family,data,id, waves,corstr,cluster, reps=4){
environment(formula) <- environment() #associate the correct environment with the formula
geeglm(formula,family,data,id = id, waves = waves, corstr = corstr)
}
clusbootreg(formula=Outcome~Treatment+Time+Time*Treatment,
family=Gamma(link = "log"),
data=testDF,id=testDF$ID,waves=testDF$Time,
cluster=data$Cluster, reps=4, corstr = "independence")
#works