JenniNiku / gllvm

Generalized Linear Latent Variable Models
https://jenniniku.github.io/gllvm/
48 stars 20 forks source link

formula argument does not allow for transformation of predictors #78

Closed BertvanderVeen closed 1 year ago

BertvanderVeen commented 1 year ago

An example is:

data(spider)
gllvm(spider$abund,spider$x,formula=~log1p(fallen.leaves),family='poisson')

which gives as output:Error in scale(fallen.leaves) : object 'fallen.leaves' not found. Any function of predictors will do, e.g., the same happens with scale.

The formula argument for the latent variables does not have the same issue: model <- gllvm(y = spider$abund, X = spider$x, lv.formula = ~log(soil.dry) + log1p(bare.sand) + log1p(fallen.leaves) + log1p(moss) + log1p(herb.layer) + log1p(reflection), num.RR = 2, family = "poisson") works just fine.

JenniNiku commented 1 year ago

Should work now, but I may need to do a few more checks

smithja16 commented 1 year ago

I know this is closed, but I find the same problem when trying to use 'poly(x, degree=2)' in a formula. Sometimes I get the error 'unexpected numeric constant' and sometimes '$ operator is invalid for atomic vectors', depending (it seems) on what family I specify.

BertvanderVeen commented 1 year ago

Thanks for posting, James. Unfortunately I cannot reproduce your issue, can you provide me with an example? Then I will look into it. What version of the package are you using?

smithja16 commented 1 year ago

I'm using gllvm_1.3.1 (and unfortunately R 4.1.0, bc my organization is slow approving software). This code fails for me: Picture1

BertvanderVeen commented 1 year ago

That makes sense, this was fixed in a later version. You can work around by setting up the X matrix including the predictors that you want in the model, and without using the formula argument. So something along the lines of:

newX = data.frame(poly(X$glorys_sst, degree = 2, raw = TRUE), X$Area_swept_km2)
model <- gllvm(Y, X = newX, num.lv = 1, family="tweedie")

But please refrain from commenting on closed issues; start a new issue if you think you have found a bug, and a topic in the discussion forum otherwise.