使用regr()命令:选择未定义的列时出错

发布于 2025-01-23 23:50:35 字数 591 浏览 3 评论 0原文

尝试运行 ()命令来自yhat package

Error in `[.data.frame`(new.data, , c(DV, IVx)) : 
  undefined columns selected

这是我正在使用的代码:

DEregr_model <- lm(TotalBiomass ~ propnC + propnV + propnR + I(propnC^2) + I(propnV^2) + propnC:propnV + propnV:propnR + propnV:I(propnC^2), DE_model)
DEregrout <- regr(DEregr_model)

为什么此函数返回错误?

I get the following error when trying to run the regr() command from the yhat package:

Error in `[.data.frame`(new.data, , c(DV, IVx)) : 
  undefined columns selected

Here is the code I'm using:

DEregr_model <- lm(TotalBiomass ~ propnC + propnV + propnR + I(propnC^2) + I(propnV^2) + propnC:propnV + propnV:propnR + propnV:I(propnC^2), DE_model)
DEregrout <- regr(DEregr_model)

Why is this function returning an error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

审判长 2025-01-30 23:50:35

我想我可以在此MCVE的评论中表达我的怀疑:

> lm.gas <- lm( mpg ~ hp + disp +hp:I(disp^2), data= mtcars)
> lm.gas

Call:
lm(formula = mpg ~ hp + disp + hp:I(disp^2), data = mtcars)

Coefficients:
 (Intercept)            hp          disp  hp:I(disp^2)  
   3.562e+01    -4.168e-02    -5.879e-02     3.151e-07  

> install.packages("yhat")
also installing the dependency ‘yacca’


> library(yhat)
> regr(lm.gas)
Error in `[.data.frame`(new.data, , c(DV, IVx)) : 
  undefined columns selected
In addition: Warning message:
In regr(lm.gas) : NAs introduced by coercion

我怀疑i(。)术语并未以lm的结果保存,以的方式来调用。 regr功能能够处理。

围绕的工作将是在增强数据集中使用单独的名称计算平方变量的值。

I think I can demonstrate my suspicion expressed in the comments with this MCVE:

> lm.gas <- lm( mpg ~ hp + disp +hp:I(disp^2), data= mtcars)
> lm.gas

Call:
lm(formula = mpg ~ hp + disp + hp:I(disp^2), data = mtcars)

Coefficients:
 (Intercept)            hp          disp  hp:I(disp^2)  
   3.562e+01    -4.168e-02    -5.879e-02     3.151e-07  

> install.packages("yhat")
also installing the dependency ‘yacca’


> library(yhat)
> regr(lm.gas)
Error in `[.data.frame`(new.data, , c(DV, IVx)) : 
  undefined columns selected
In addition: Warning message:
In regr(lm.gas) : NAs introduced by coercion

I suspect that the I(.) terms are not being saved in the result of the lm call in a manner that the regr function is able to handle.

The work around would be to calculate the values of the squared variables with separate names in an augmented dataset.

旧梦荧光笔 2025-01-30 23:50:35

根据评论,我发现了问题。交互项(即i(propnv^2))的函数未正确读取。因此,我在数据框架中添加了其他列的平方值,以便该模型将这些术语读取为单个值,而不是试图将它们分开。校正的代码如下:

## make new columns for interaction effect of seeding rate propn
DE$propnC2 <- DE$propnC^2
DE$propnV2 <- DE$propnV^2
DE$propnR2 <- DE$propnR^2

## run lm model with adjusted terms
DEregr_model <- lm(TotalBiomass ~ propnC + propnV + propnR + propnC2 + propnV2 + propnC:propnV + propnV:propnR + propnV:propnC2, DE_model) 
DEregrout <- regr(DEregr_model)

Regr()函数现在没有错误运行,感谢您的输入!

Based on the comments, I figured out the issue. The interaction terms (i.e., I(propnV^2)) weren't being read correctly by the function. So I added additional columns in my data frame with the squared values, so that the model was reading these terms as individual values, not trying to separate them. Corrected code is below:

## make new columns for interaction effect of seeding rate propn
DE$propnC2 <- DE$propnC^2
DE$propnV2 <- DE$propnV^2
DE$propnR2 <- DE$propnR^2

## run lm model with adjusted terms
DEregr_model <- lm(TotalBiomass ~ propnC + propnV + propnR + propnC2 + propnV2 + propnC:propnV + propnV:propnR + propnV:propnC2, DE_model) 
DEregrout <- regr(DEregr_model)

The regr() function now runs without error, thanks everyone for your input!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文