首页 > 解决方案 > 无法从 R 循环中的 lqmm 函数中提取 p_value

问题描述

我正在使用 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])
}

不幸的是,它没有用。错误是“错误:'symbol' 类型的对象不是子集”。

但是如果我只打印两个系数,下面的代码就可以了:

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 值。我未能在谷歌中找到答案。有人可以帮我一个忙吗?

非常感谢你的帮助。

标签: rmodelextract

解决方案


添加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

推荐阅读