r - 变量 ‘group’ 已在 ‘linfct’ 中指定,但在 ‘model’ 中找不到!

标签 r command-line-arguments lm

我的问题已在之前的论坛上提出,但出于某种原因,答案对我不起作用。我正在使用 R 对日志转换数据进行计划比较,但我继续收到错误消息: mcp2matrix(model, linfct = linfct) 错误: 变量“group”已在“linfct”中指定,但在“model”中找不到! 任何帮助,将不胜感激。谢谢!

这是我的数据:

stress.data = read.table(textConnection(" group    rate   lnrate
1      P  69.169 4.236553
2      P  68.862 4.232105
3      C  84.738 4.439564
4      F  99.692 4.602085
5      C  87.231 4.468560
6      C  84.877 4.441203
7      P  70.169 4.250907
8      P  64.169 4.161520
9      P  58.692 4.072303
10     C  80.369 4.386629
11     C  91.754 4.519111
12     P  79.662 4.377793
13     C  87.446 4.471021
14     C  87.785 4.474891
15     P  69.231 4.237449
16     P  75.985 4.330536
17     F  91.354 4.514742
18     C  73.277 4.294247
19     F  83.400 4.423648
20     F 100.877 4.613902
21     C  84.523 4.437024
22     F 102.154 4.626481
23     C  77.800 4.354141
24     C  70.877 4.260946
25     P  86.446 4.459520
26     P  97.538 4.580242
27     F  89.815 4.497752
28     F  80.277 4.385483
29     P  85.000 4.442651
30     F  98.200 4.587006
31     C  90.015 4.499976
32     F 101.062 4.615734
33     F  76.908 4.342610
34     C  99.046 4.595584
35     F  97.046 4.575185
36     P  69.538 4.241873
37     C  75.477 4.323828
38     C  62.646 4.137500
39     P  70.077 4.249595
40     F  88.015 4.477507
41     F  81.600 4.401829
42     F  86.985 4.465736
43     F  92.492 4.527122
44     P  72.262 4.280298
45     P  65.446 4.181225"), header = TRUE)

library("multcomp")
stress.lm= lm(stress.data$lnrate ~ stress.data$group, data = stress.data)
stressPlanned= glht(stress.lm, linfct=mcp(group=c("C-P=0", "F-P=0")))

mcp2matrix(model, linfct = linfct) 错误: 变量“group”已在“linfct”中指定,但在“model”中找不到!

最佳答案

问题来自于在公式中使用 $ 访问数据,而不是按预期从 data 参数中获取数据。通常不鼓励这样做,因为它会混淆后续函数,例如您的情况下的 predict()glht() 。只需使用:

stress.lm <- lm(lnrate ~ group, data = stress.data)
glht(stress.lm, linfct = mcp(group=c("C-P=0", "F-P=0")))
##   General Linear Hypotheses
## 
## Multiple Comparisons of Means: User-defined Contrasts
## 
## Linear Hypotheses:
##            Estimate
## C - P == 0   0.1180
## F - P == 0   0.2215

关于r - 变量 ‘group’ 已在 ‘linfct’ 中指定,但在 ‘model’ 中找不到!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47723572/

相关文章:

r - 使用R中的iconv函数音译德语单词

python - 如果目录路径作为命令行参数传递给 python,将执行哪个 python 脚本?

python - 使用 Python3 和 argparse 模块,如果传递了不同的参数,我可以禁止该参数吗?

r - R 中系数的不同 NA 操作和线性模型总结

r - 如何叠加两个geom_bar?

r - 如何在 R 中计算已滚动的股票投资组合的百分比?

r - 外部函数调用中的 NA/NaN/Inf (arg 5)

c - 我应该如何使用命令行参数从文件中填充整数数组?文件的大小和编号。元素的数量可能会有所不同

r - 修改 lm 包装函数中的权重参数

r - 如何将 lm 对象存储在 R 的数据框中