r - 无法从 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 值。我未能在谷歌中找到答案。有人可以帮我一个忙吗?
非常感谢你的帮助。
解决方案
添加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
推荐阅读
- filter - 当字段值四舍五入时,ag-grid 数字过滤器不起作用
- python - 使用 tck = interpolate.bisplrep(x, y, z, kx=kx, ky=ky) 使用 x 和 y 值数组获取 z 值
- string - 返回一个数组
? 在科特林 - javascript - 处理 document.location (href) 更改
- django - django rest框架访问和编辑嵌套模型
- python - 如何使用 python 绘制带有形状文件和纬度长网格线的 geoTiff 文件?
- c# - 使用 System.Text.Json 将 JSON 反序列化为对象
- sql - 如何在 SQL Server 中使用 where 子句计算行数?
- java - 一个 Glassfish3 实例发生异常,但另一个实例没有:“java.lang.String 无法转换为 org.slf4j.Marker”
- html - 仅当鼠标悬停在图像中心时,在图像顶部显示链接并降低图像不透明度?