R - 将列名称传递为变量,名称包含I()

问题描述 投票:1回答:1

我正在执行多项式回归并测试系数的线性组合。但是当我试图测试系数的线性组合时,我遇到了一些问题。

LnModel_1 <- lm(formula = PROF ~ UI_1+UI_2+I(UI_1^2)+UI_1:UI_2+I(UI_2^2)) 
summary(LnModel_1)

它输出以下值:

Call:
lm(formula = PROF ~ UI_1 + UI_2 + I(UI_1^2) + UI_1:UI_2 + I(UI_2^2))
Residuals:
 Min      1Q  Median      3Q     Max 
-3.4492 -0.5405  0.1096  0.4226  1.7346 
Coefficients:
Estimate Std. Error t value Pr(>|t|)    
(Intercept)  4.66274    0.06444  72.354  < 2e-16 ***
UI_1         0.25665    0.07009   3.662 0.000278 ***
UI_2         0.25569    0.09221   2.773 0.005775 ** 
I(UI_1^2)   -0.15168    0.04490  -3.378 0.000789 ***
I(UI_2^2)   -0.08418    0.05162  -1.631 0.103643    
UI_1:UI_2   -0.02849    0.05453  -0.522 0.601621

然后我使用名称(coef())来提取系数名称

names(coef(LnModel_1))
output:
[1] "(Intercept)" "UI_1"        "UI_2"        "I(UI_1^2)" 
"I(UI_2^2)""UI_1:UI_2" 

出于某些原因,当我使用glht()时,它会在UI_2 ^ 2上给出错误

slope <- glht(LnModel_1, linfct = c("UI_2+ UI_1:UI_2*2.5+ 2*2.5*I(UI_2^2) =0
") )
Output:
Error: multcomp:::expression2coef::walkCode::eval: within ‘UI_2^2’, the term
‘UI_2’ must not denote an effect. Apart from that, the term must evaluate to
a real valued constant

不知道为什么它会给我这个错误信息。如何将I(UI_2 ^ 2)系数输入到glht()

非常感谢你

r non-linear-regression coefficients hypothesis-test
1个回答
0
投票

问题似乎是I(UI^2)可以被解释为R中的表达,就像你在这里做的那样LnModel_1 <- lm(formula = PROF ~ UI_1+UI_2+I(UI_1^2)+UI_1:UI_2+I(UI_2^2))

因此,您应该指出要在字符串中评估Rstring

slope <- glht(LnModel_1, linfct = c("UI_2+ UI_1:UI_2*2.5+ 2*2.5*\`I(UI_2^2)\` =0
") )

检查我的例子(因为我无法重现你的问题):

library(multcomp)

cars <- copy(mtcars)
setnames(cars, "disp", "UI_2")
model <- lm(mpg~I(UI_2^2),cars)

names(coef(model))

slope <- glht(model, linfct = c("2*2.5*\`I(UI_2^2)\` =0") )
© www.soinside.com 2019 - 2024. All rights reserved.