lamho86 / phylolm

GNU General Public License v2.0
30 stars 12 forks source link

`predict.phylolm` fails for `phylostep` models #73

Open AMBarbosa opened 4 days ago

AMBarbosa commented 4 days ago

When using the newdata argument, predict.phylolm() fails for models built with phylostep(). Here's a reprex based on the ?phylostep example (using the same data for modelling and predicting here, but normally I want to predict beyond that, so I do need to use newdata):

# example data:

set.seed(123456)
tre = rcoal(100)
taxa = sort(tre$tip.label)
b0=0; b1=1;
x1 = rTrait(phy=tre,model="BM",
            parameters=list(ancestral.state=0,sigma2=10))
x2 = rTrait(phy=tre,model="BM",
            parameters=list(ancestral.state=0,sigma2=10))
x3 = rTrait(phy=tre,model="BM",
            parameters=list(ancestral.state=0,sigma2=10))
y <- b0 + b1*x1 + rTrait(n=1,phy=tre,model="BM",
                         parameters=list(ancestral.state=0,sigma2=1))
dat <- data.frame(trait=y[taxa],pred1=x1[taxa],pred2=x2[taxa],pred3=x3[taxa])

# model and predict:

fit_step <- phylostep(trait~pred1+pred2+pred3,
                      data=dat,phy=tre)

predict(fit_step, dat)

# Error: $ operator is invalid for atomic vectors

The error doesn't occur if the model is computed with phylolm() and the formula is specified manually:

fit_lm <- phylolm(trait~pred1+pred2,
                  data=dat,phy=tre)

predict(fit_lm, dat)

However, if I try to automate extraction of the selected variables from the phylostep model object, error again:

fit <- phylolm(fit_step$formula,data=dat,phy=tre)

predict(fit, dat)

Regards,

lamho86 commented 4 days ago

Thanks for informing me. This bug is now fixed.