Extracting regression results

Welcome to the forum for R2MLwiN users. Feel free to post your question about R2MLwiN here. The Centre for Multilevel Modelling take no responsibility for the accuracy of these posts, we are unable to monitor them closely. Do go ahead and post your question and thank you in advance if you find the time to post any answers!

Go to R2MLwiN: Running MLwiN from within R >> http://www.bris.ac.uk/cmm/software/r2mlwin/
tjsduq64
Posts: 31
Joined: Mon Jul 15, 2019 10:04 pm

Extracting regression results

Hello,

what are some package options for extracting regression coefficients for R2MLwiN? I find that some packages won't work because they don't extract from S4 class. This is a type of models that I am running (i.e., two level random intercept model). I want to extract fixed and random effects coefficients and their standard errors, p-value, etc.

sample <- x36_i
model <- bmi ~ 1 + female + age3 + age4 + lowincome + black + hispanics + (1 | strata) + (1 | id)
option <- list(weighting = list(weightvar = c(NA, "w_mec"), standardised=TRUE))
result_x36_main <- runMLwiN(Formula = model,
estoptions = option,
data = sample)
summary(result_x36_main)

Thank you,

Sun
ChrisCharlton
Posts: 1282
Joined: Mon Oct 19, 2009 10:34 am

Re: Extracting regression results

Which packages are you trying? We might be able to investigate whether it is possible to add support in a later version of R2MLwiN.

You should be able to calculate these values yourself as follows:

Code: Select all

``````# coefficients
coef(result_x36_main)

# standard errors
sqrt(diag(vcov(result_x36_main)))

# z-score
coef(result_x36_main)/sqrt(diag(vcov(result_x36_main)))

# p-values
2 * pnorm(abs(coef(result_x36_main)/sqrt(diag(vcov(result_x36_main)))), lower.tail = FALSE)

# 95% C.I. (lower)
coef(result_x36_main) - qnorm(.975) * sqrt(diag(vcov(result_x36_main)))

# 95% C.I. (upper)
coef(result_x36_main) + qnorm(.975) * sqrt(diag(vcov(result_x36_main)))
``````
tjsduq64
Posts: 31
Joined: Mon Jul 15, 2019 10:04 pm

Re: Extracting regression results

ChrisCharlton wrote: Fri Aug 16, 2019 3:47 pm Which packages are you trying? We might be able to investigate whether it is possible to add support in a later version of R2MLwiN.

You should be able to calculate these values yourself as follows:

Code: Select all

``````# coefficients
coef(result_x36_main)

# standard errors
sqrt(diag(vcov(result_x36_main)))

# z-score
coef(result_x36_main)/sqrt(diag(vcov(result_x36_main)))

# p-values
2 * pnorm(abs(coef(result_x36_main)/sqrt(diag(vcov(result_x36_main)))), lower.tail = FALSE)

# 95% C.I. (lower)
coef(result_x36_main) - qnorm(.975) * sqrt(diag(vcov(result_x36_main)))

# 95% C.I. (upper)
coef(result_x36_main) + qnorm(.975) * sqrt(diag(vcov(result_x36_main)))
``````
Thank you so much for providing these codes! A package will be nice since I have multiple models to run, but I will have to use these for now unless others have better ideas.

I am pretty new to R as well so I tried several packages - stargazer, texreg, and sjPlot.

stargazer gives "Error: \$ operator not defined for this S4 class"
texreg doesn't specifically support R2MLwiN object, although it provides a table with fixed effect coefficients.
tab_model function in sjPlot gives "Could not access model information.Error: \$ operator is invalid for atomic vectors"

Thank you.

Edit: I just realized that texreg does work and provides fixed and random effects coefficients. I will try to use this and confirm if this is sufficient.

Sun
ChrisCharlton
Posts: 1282
Joined: Mon Oct 19, 2009 10:34 am

Re: Extracting regression results

Thanks for the references. We did work on getting texreg to work with R2MLwiN, see https://www.bristol.ac.uk/cmm/media/r2m ... ide04.Rout for another example of using this, as well as the memisc package. Last time I looked at stargazer it didn't provide the option to extend it for more estimation commands, but I will have another look to see whether this has changed. I am not familiar with sjPlot, so I will take a look at whether there are options for linking with this.
tjsduq64
Posts: 31
Joined: Mon Jul 15, 2019 10:04 pm

Re: Extracting regression results

ChrisCharlton wrote: Fri Aug 16, 2019 4:33 pm Thanks for the references. We did work on getting texreg to work with R2MLwiN, see https://www.bristol.ac.uk/cmm/media/r2m ... ide04.Rout for another example of using this, as well as the memisc package. Last time I looked at stargazer it didn't provide the option to extend it for more estimation commands, but I will have another look to see whether this has changed. I am not familiar with sjPlot, so I will take a look at whether there are options for linking with this.
Thank you! texreg worked for me.