3-level logistic regression - r

I am trying to analyze a longitudinal data with binary response using 3-level logistic regression. Hierarchical structure of my data looks like this example- students (level 1) are nested within a class(level 2) and the classes are nested within a school (level 3).
Can anyone suggest me some appropriate readings on the SAS or R codes for performing 3-level mixed effects logistic regression? I am not requesting helps regarding the theory behind 3-level logistic regression. Any examples with SAS or R codes would be of great help.

In R you can use the glmer() function from the lme4 package for mixed effects models. You specify the effects in the formula= option. For nested random effects you probably want something like:
model <- glmer(formula = response ~ (1|student|class/block), family=binomial)
There are some examples of multi-level models on R-sessions
and you can fit non-linear responses, such as binomial, with family=binomial.

Related

Ordinal logistic regression (or Beta regression) with a LASSO regularization in R?

I was wondering if someone would know an R package that would allow me to fit an Ordinal Logistic regression with a LASSO regularization or, alternatively, a Beta regression still with the LASSO? And if you also know of a nice tutorial to help me code that in R (with appropriate cross-validation), that would be even better!
Some context: My response variable is a satisfaction score between 0 and 10 (actually, values lie between 2 and 10) so I can model it with a Beta regression or I can convert its values into ranked categories. My interest is to identify important variables explaining this score but as I have too many potential explanatory variables (p = 12) compared to my sample size (n = 105), I need to use a penalized regression method for model selection, hence my interest in the LASSO.
The ordinalNet package does this. There's a paper with example here:
https://www.jstatsoft.org/article/download/v099i06/1440
Also the glmnetcr package: https://cran.r-project.org/web/packages/glmnetcr/vignettes/glmnetcr.pdf

how to run a GLMM on R

I am trying to run a Generalized linear mixed model (GLMM) on r, I have two fixed factors and two random factors
however there are a lot of holes in my data set and the I am struggling to find a code to run the glmm all I found is the glm
Can someone please walk me through this, I know very little about R and coding
You can use the lme4 package as well. The command for a generalized linear mixed model is glmer().
Example:
install.packages("lme4") #If you still haven't done it.
library(lme4)
myfirstmodel <- glmer(variable_to_study ~ fixed_variable + (1|random_effect_varible), data = mydataset, family = poisson)
Family = poisson was just an example. Choose the 'family' according to the nature of the variable_to_study (eg. poisson for discrete data).

non nested model comparison using R

To explain my problem , i have this simulated data using R.
require(splines)
x=rnorm(20 ,0,1)
y=rep(c(0,1),times=10)
First i fitted a regular (linear effects) logistic regression model.
fit1=glm(y~x ,family = "binomial")
Then to check the non linear effects, i fitted this natural spline model .
fit2=glm(y~ns(x,df=2) ,family = "binomial")
Based on my thinking models , i believe these 2 models are non nested models.
Next i wanted check whether the non linear model (fit2) has any significant effects compared to the regular logistic model (fit1).
Is there any way to compare this two models? I believe i cannot use the lrtest function in lmtest package, because these two models are not nested models.
Any suggestion will be highly appreciated
Thank you.

Moving from SPSS to R: Defining a two-intercept model in gls with random effect and repeated measure

I am new to R and am trying to reproduce results from my SPSS analyses, but seem to be missing something.
I am trying to run a linear mixed effects model using gls in the nlme package.
The SPSS syntax I am trying to reproduce is:
MIXED Satisfaction_A BY Role
/FIXED=Role | NOINT SSTYPE(3)
/METHOD=ML
/PRINT=SOLUTION TESTCOV
/RANDOM=Role | SUBJECT(focalid) COVTYPE(UNR)
/REPEATED=Role | SUBJECT(dyadid*focalid) COVTYPE(UNR).
Essentially, it is a two-intercept model with nested data where Focal ID is the level-2/nesting variable which contains 2 responses for Satisfaction, distinguished by Role.
The R code I have so far is:
gls(Satisfaction_A ~ Role -1, #Two-intercept approach
data = chlpairwise,
correlation = corSymm(form = ~1|focalid/dyadid),
weights = varIdent(form = ~1|Role),
method = "ML",
na.action = na.omit)
The regression coefficients are very similar to those from SPSS. But what am I missing in the code so that I can view the Covariance Parameters like in SPSS? SPSS Covariance Parameters
Many thanks! I hope to keep learning so that I can eventually give back to this community for all of the help I have received. :)
Probably you need to use the function getVarCov() that returns the marginal covariance matrix from a fitted marginal model. It will also work if you fit a linear mixed effects model using function lme().

f-test for two models in R

I would like to compare two models using f-test fitting my data. For each model I performed Monte-Carlo simulation that provided statistical estimation for each model parameter and rms fit error. I would like to use f-test in R to determine which model is preferable.
Best to use the anova function.
anova(modle1, model2)
This preforms a model f test.

Resources