简体   繁体   中英

"Error in model.matrix.formula(form, data) : data must be a data.frame" while doing a GAM (mgcv package) in R

Asking for a friendly soul that knows how to fix this error or that understands why this is appearing.

Not exactly sure what happened but this error appears every time I try to do a GAM with random effects ( bs="re" , with mgcv package). This is strange since appears not only to new models but even to models that previously worked (multiple times). I made sure the data has no NA's, scientific data, or random formulas. Also, I am not using the date format to avoid errors has previously worked as it is.

I also tried to transform the data into a data frame via as.data.frame(x) but the same error occurred.

I have been playing a bit with the formula and it appears that every time the random effects bs="re" are present, either the 2 of them (Site, State) or only one of them (Site), it is when the error occurs. If I take them completely out of the formula it works perfectly.

I am thinking that could be:

  1. Some incompatibility with another package that I may have installed but tried to solve this with no effect. Removed all the most recently installed packages and the error persisted.

  2. Other could be any update to the mgcv package?

Update: It works in R software just not in R studio.

Does anyone have an idea on how to fix this or why this is appearing

The following model was previously working but not anymore, giving me every single time the mentioned error

gam_2a <- gam(Total_Items ~ s(DayI0, k=14) + s(Site, State, bs="re"), offset(log(EffortDayC)),data = x,family=poisson(link="log"),method = "REML")

Description of the variables:

Total_Items = Number of items of debris found per event;

DayI0 = Number of days since first clean up (numeric);

Site = Site of sampling (Sites are within States);

State = State of sampling;

EffortDayC = Effort(Length of the beach, number of volunteers, duration of sampling)*DayC(interval of sampling);

The str(x) output below:

enter image description here

And the head of the data to understand a bit better: enter image description here enter image description here

Sorted! the package agricolae was causing some incompatibility with the mgcv package.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM