'Error when using regr() command: undefined columns selected

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?



Solution 1:[1]

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.

Solution 2:[2]

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!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 IRTFM
Solution 2 Helen B