我使用R中的lqmm函数通过循环运行多个模型我希望从每个模型中获取P值。这是一个包含两个因变量的简单数据集。
data <- structure(list(y1 = c(1, 1, 4, 4, 5, 5, 7, 7), y2 = c(1, 2, 1, 2, 7, 8, 7, 8), x = c(1, 2, 3, 4, 5, 6, 7, 8), id = c(1L, 1L, 2L, 2L, 2L, 3L, 4L, 5L), weight = c(0.9401069, 1.2730856, 0.985858, 4.1602805, 1.6042408, 1.0452717, 0.9784276, 1.5199646)), class = "data.frame", row.names = c(NA,-8L))
首先,我运行单个模型并使用以下代码提取P值:
model <- lqmm(fixed= y1 ~ x, random = ~1, tau=0.5, group=id,data=data,weights=data$weight,na.action=na.omit)
summary(model)$tTable[2,5]
它运作良好。
由于我有多个因变量,我需要使用循环函数来运行它们。这是我试图自动运行它们。
for (j in 1:2) {
form <- formula(paste(names(data)[j], "~ x" ))
model <- lqmm(form, random=~1, group=id, weights=data$weight, data=data, tau=0.5, na.action=na.omit)
print(summary(model)$tTable[2,5])
}
不幸的是,它没有用。错误是“错误:类型'符号'的对象不是子集”。
但是,如果我只打印两个系数,下面的代码工作:
for (j in 1:2) {
form <- formula(paste(names(data)[j], "~ x" ))
model <- lqmm(form, random=~1, group=id, weights=data$weight, data=data, tau=0.5, na.action=na.omit)
print(coef(model))
}
但我需要的信息是P值。我没能在谷歌找到答案。有人帮我一个忙吗?
非常感谢你的帮助。
在eval()
周围添加form
:
set.seed(1234)
for(j in 1:2) {
form <- formula(paste(names(data)[j], "~ x" ))
model <- lqmm(eval(form), random=~1, group=id, weights=data$weight, data=data, tau=0.5, na.action=na.omit)
print(summary(model)$tTable[2,5])
}
#[1] 7.055214e-07
#[1] 0.06919567