[R-meta] Accounting for uncertainty in a mixed-effects regression
Viechtbauer Wolfgang (SP)
wolfgang.viechtbauer at maastrichtuniversity.nl
Tue Feb 6 19:35:15 CET 2018
There is some (convoluted) code in predict.rma() that allows matching of terms by names. So, for example, if the variables in the model are called "X", "Y", and "Z", then one could do 'newmods = c(Z=3, X=1, Y=2)' or any other order and the function puts things in the correct order. In fact, it does partial matching, so if the variables are called "Xblah", "Yblah", "Zblah", then 'newmods = c(Z=3, X=1, Y=2)' would still work. There was a bug in that part of the code that would trip up the partial matching when there are interactions. I fixed that a while ago in the 'devel' version. Make sure to install this:
https://github.com/wviechtb/metafor#installation
With this installed, I have no problems running this (and as you already figured out, since predict() by default adds the intercept automatically, it needs to be removed from what you feed to newmods):
### silly madeup test data
k <- 500
dat <- data.frame(yi = rnorm(k, 0, 1),
vi = rep(.01, k),
MAP = runif(k, 0, 1),
MAT = runif(k, 0, 1),
CO2dif = runif(k, 0, 1),
Biome = sample(c("ARef", "Boreal_Forest", "Tropical_Forest"), k, replace=TRUE))
res <- rma(yi, vi, mods = ~ MAP + MAT*CO2dif + Biome, data=dat)
res
X <- model.matrix(~ MAP + MAT*CO2dif + Biome, data=dat)
X <- X[,-1]
sav <- predict(res, newmods = X)
sav
Best,
Wolfgang
>-----Original Message-----
>From: Michael Dewey [mailto:lists at dewey.myzen.co.uk]
>Sent: Tuesday, 06 February, 2018 19:08
>To: Cesar Terrer Moreno; Viechtbauer Wolfgang (SP)
>Cc: r-sig-meta-analysis at r-project.org
>Subject: Re: [R-meta] Accounting for uncertainty in a mixed-effects
>regression
>
>Dear Cesar
>
>I wonder whether this is because partial matching is tripping you up as
>the name of the main effect partial matches the interaction. I do not
>think having a constant value for a predictor variable is going to be
>the issue.
>
>Michael
>
>On 06/02/2018 15:46, Cesar Terrer Moreno wrote:
>> I noticed predict.rma works fine when I exclude the interaction from
>both the model and newmods. I have thus the feeling there might be
>something wrong with “CO2dif”, which I have forced to always have a value
>of 300, instead of varying as the other moderators, because I want a
>static picture at CO2dif=300. Therefore, CO2dif is not variable.
>>
>> I have reformulated my question to reflect this problem.
>>
>> This is my model
>>
>> ### MODEL ###
>> summary(ECMmv <- rma(es, var, data=df.ecm, mods= ~ MAP + MAT*CO2dif +
>Biome))
>>
>> And the newmods matrix, built from a dataset (s.df.ecm) that contains
>data for all predictors on a per pixel basis, with CO2dif as a constant
>300.
>>
>> ### NEWMODS ###
>> s.df.ecm <- s.df %>% dplyr::select(x, y, MAT = temperature, MAP =
>precipitation, Biome) %>%
>> mutate(CO2dif = 300)
>> ECM.matrix <- model.matrix(~ MAP + MAT*CO2dif + Biome, data=s.df.ecm)
>>
>> # Example of ECM.matrix:
>>
>> ECM.matrix[47881:47885,-1]
>> MAP MAT CO2dif BiomeBoreal_Forest BiomeTropical_Forest
>MAT:CO2dif
>> 47881 45.13125 2.2 300 0 0
>660
>> 47882 47.75000 2.2 300 0 0
>660
>> 47883 51.20000 2.2 300 0 0
>660
>> 47884 55.93125 2.2 300 0 0
>660
>> 47885 61.94375 2.2 300 0 0
>660
>>
>> ### PREDICT ###
>> ECMpred.mv <- predict(ECMmv, newmods = ECM.matrix[,-1])
>> Error in predict.rma(ECMmv, newmods = ECM.matrix[, -1]) :
>> Multiple matches for the same variable name.
>>
>> This doesn’t work. How can I use predict with a model with an
>interaction in which one of the terms is fixed at a certain value?
>> Thanks
>>> On 6 Feb 2018, at 12:14, Cesar Terrer Moreno <cesar.terrer at me.com>
>wrote:
>>>
>>> Dear Wolfgang,
>>>
>>> I seem to be close to running your suggested analysis, but I get an
>error in predict.rma:
>>>
>>> ### MODEL ###
>>> summary(ECMmv <- rma(es, var, data=df.ecm, mods= ~ MAP + MAT*CO2dif +
>Biome))
>>>> colnames(ECMmv$X)[-1]
>>> [1] "MAP" "MAT" "CO2dif"
>>> [4] "BiomeBoreal_Forest" "BiomeTropical_Forest" "MAT:CO2dif”
>>>
>>> ### GLOBAL DATASET ###
>>> ECM.matrix <- model.matrix(~ MAP + MAT*CO2dif + Biome, data=s.df.ecm)
>>>> colnames(ECM.matrix)[-1]
>>> [1] "MAP" "MAT" "CO2dif"
>>> [4] "BiomeBoreal_Forest" "BiomeTropical_Forest" "MAT:CO2dif”
>>>
>>> ### PREDICT ###
>>> ECMpred.mv <- predict(ECMmv, newmods = ECM.matrix[,-1])
>>> Error in predict.rma(ECMmv, newmods = ECM.matrix[, -1]) :
>>> Multiple matches for the same variable name.
>>>
>>> As you can see, the names of the columns match in the model and
>newmods, so I am not really sure what this error means.
>>>
>>> Many thanks for your help.
>>> Best wishes,
>>> Cesar
More information about the R-sig-meta-analysis
mailing list