Specifying growth mixture model with lcmm() - r

I am trying to replicate some growth mixture modeling results from Mplus in R, using the lcmm() package. I am having some trouble converting the model specification from a Mplus framework to a linear mixed effects model framework in R.
In Mplus, a 2-class growth mixture model was specified like this:
VARIABLE: NAMES ARE y1-y4;
CLASSES = c(2);
ANALYSIS: TYPE = MIXTURE;
MODEL:
%OVERALL%
i s | y1#0 y2#1 y3#2 y4#3;
OUTPUT:
TECH1;
In R, using the lcmm() package, I think the model specification should look something like this:
m1 <- hlme(y ~ time,
mixture = ~ time,
random = ~ time,
subject = 'id',
ng = 2,
idiag = F,
data = dat.long)
However, I am not sure if I am specifying the mixture and random arguments correctly. Any suggestions would be appreciated!
Data is here: http://www.statmodel.com/usersguide/chap8/ex8.1.dat

Related

About Constrained Mixed Effects Models

Currently I want to fit a mixed effects model with positive or negative constraints on the parameters.
For parameter estimation using Joint Modeling, we use nlme package, which is required by the JM package.
In the lme function of the nlme package, it seems that optimization details can be specified by using lmeControl().
https://stat.ethz.ch/R-manual/R-devel/library/nlme/html/lmeControl.html
In "L-BFGS-B" using the optim function described as an option here,
It looks like we can specify the maximum and minimum values of the estimated parameters.
https://stat.ethz.ch/R-manual/R-devel/library/stats/html/optim.html
However, only "optim" or "nlminb" as character can be specified with lmeControl(),
It doesn't look like I can write an optimization function by reading the source code.
https://github.com/cran/nlme/blob/master/R/lme.R
Is it possible to create a parameter constrained mixed effects model using the nlme package?
Thank you.
memo:
Specifying upper and lower options to the lme and lmeCntrol functions was not effective.
Also giving the lmeCntrol function an optimization function didn't work.
(Maybe I could not write the funciton properly.)
require(JM)
require(nlme)
require(tidyverse)
data(aids)
data(aids.id)
#fit mixed effect model
fitLME <- lme(sqrt(CD4) ~ obstime + obstime:drug, random = ~ obstime | patient, data = aids, )
fitLME %>% summary()
#create lme control.
ctrl <- lmeControl(optimMethod = "L-BFGS-B", opt ="optim", lower = c(0,0, 0), upper = c(1,20,1))
#fit with lme control
fitLME_ctrl <- lme(sqrt(CD4) ~ obstime + obstime:drug, random = ~ obstime | patient, data = aids, control = ctrl)
#this does not change result.
fitLME_ctrl %>% summary()

Cluster robust standard errors for mixed effect/LMER models?

I'm estimating a mixed effects model using simulated data. The basis of this is a conjoint experiment: there are N number of countries in the study with P participants and each respondent is shown the experiment twice. This means that there are NxPx2 observations. Heterogeneity is introduced into the data at the country level and so I run a mixed effect model using lmer with random effects varying by country to account for this variance. However, because each respondent does the experiment twice, I also want to cluster my standard errors at the individual level. My data and model looks something like this:
library(lme4)
data(iris)
# generating IDs for observations
iris <- iris %>% mutate(id = rep(1:(n()/2), each = 2))
#run model
mod <- lmer(Sepal.Length~Sepal.Width+Petal.Length+Petal.Width + (Sepal.Width+Petal.Length+Petal.Width || Species), data=iris, REML = F, control = lmerControl(optimizer = 'bobyqa'))
I then attempt to get clustered SEs using the parameters package:
library(parameters)
param <- model_parameters(
mod,
robust = TRUE,
vcov_estimation = "CR",
vcov_type = "CR1",
vcov_args = list(cluster = iris$id)
)
This returns an error:
Error in vcovCR.lmerMod(obj = new("lmerModLmerTest", vcov_varpar = c(0.00740122363004, : Non-nested random effects detected. clubSandwich methods are not available for such models.
I'm not married to any one method or anything. I just want to return clustered SEs for this type of model specification. As of now I can't find any package that does this. Does anyone know how this can be done, or if such a model even makes sense? I'm new to MLMs but I was thinking if I were to run this as a simple linear model I would lm_robust and cluster by individual so it makes sense to me that I should do the same here as well.

Why I can't include country dummies in my fixed effects model?

First of all I build the following dataframe (country_Id as factor variable and year as numeric):
mydata = pdata.frame(mydata, index = c("country_Id","year"),row.names = TRUE)
Then I check it with:
index(mydata)
pdim(mydata)
is.pconsecutive(mydata)
class(mydata)
Everything seems to be fine but I want to include country-dummies in the fixed-effects model it does not work
femodel_1 <- plm(y~x + ldvx + factor(country_Id) , data= mydata, model = "within")
And another problem is that my random model shows that the individual variance is 0
remodel_1 <- plm(y~x + ldvx , data= mydata, model = "random")
Unfortunately I can not find the problem.
Your one-way fixed effect model already takes care of the country fixed effects. This is why you cannot add them to the model's formula again - it would not make sense- they just disappear. So you are fine with:
femodel_1 <- plm(y~x + ldvx, data= mydata, model = "within")
If you want to values of the country fixed effects, use fixef(fe_model1).
About your random effect model:
The Swamy-Arora RE estimator does not guarantee positive variance estimates (read up on this in a good econometrics text book or look here: https://stats.stackexchange.com/questions/176827/error-in-plm-random-effects-swamy-arora-swar-estimator-with-lagged-dependent/181444#181444). You can try to change your model (if it makes sense) and/or change your data a bit (e.g., more observations). Also, you can try to switch to a different RE estimator - plm offers a few.

R equivalent to Stata's xtregar

I'm doing a replication of an estimation done with Stata's xtregar command, but I'm using R instead.
The xtregar command implements the method from Baltagi and Wu (1999) "Unequally spaced panel data regressions with AR(1) disturbances" paper. As Stata describes it:
xtregar fits cross-sectional time-series regression models when the disturbance term is first-order autoregressive. xtregar offers a within estimator for fixed-effects models and a GLS estimator for random-effects models. xtregar can accommodate unbalanced panels whose observations are unequally spaced over time.
So far, for the fixed-effects model, I used the plm package for R. The attempt looks like this:
plm(data=A, y ~ x1 + x2, effect = "twoways", model = "within")
Nevertheless is not complete (comparing to xtregar description) and the results are not quite like the ones Stata provides. Furthermore, Stata's command needs to set a panel variable and a time variable, feature that's (as far as I can tell) absent in the plm environment.
Should I settle with plm or is there another way of doing this?
PS: I searched thoroughly different websites but failed to find a equivalent to Stata's xtregar.
Update
After reading Croissant and Millo (2008) "Panel Data Econometrics in R: The plm Package", specifically seccion 7.4 "Some useful 'econometric' models in nlme" I used something like this for the Random Effects part of the estimation:
gls(data=A, y ~ x1 + x2, correlation = corAR1(0, form = ~ year | pays), na.action = na.exclude)
Nevertheless the following has results closer to those of Stata
lme(data=A, y ~ x1 + x2, random = ~ 1 | pays, correlation = corAR1(0, form = ~ year | pays), na.action = na.exclude)
Try {panelAR}. This is a package for regressions in panel data that addresses AR1 type of autocorrelations.
Unfortunately, I do not own Stata, so I can not test which correlation method to replicate in panelCorrMethod
library(panelAR)
model <-
panelAR(formula = y ~ x1 + x2,
data = A,
panelVar = 'pays',
timeVar = 'year',
autoCorr = 'ar1',
rho.na = TRUE,
bound.rho = TRUE,
panelCorrMethod ='phet' # You might need to change this parameter. 'phet' uses the HW Sandwich stimator for heteroskedasticity cases, but others are available.
)

Converting a mixed model with repeated and random effects and different covariance structures from SAS to R

I have a model, created in SAS by a colleague, with a repeated effect that has an ARH1 (autoregressive heterogeneous variances) covariance structure and a random effect (with a variance components covariance structure) that I am trying to re-create in R, where I have more experience.
The original SAS code is:
PROC MIXED DATA=mylib.sep_cover_data plots=all ALPHA=0.15 CL COVTEST;
CLASS soil_grp dummy_year plot pasture;
MODEL cover = pcp soil_grp / ALPHA=0.15 CL residual SOLUTION;
RANDOM pasture / ALPHA=0.15 CL SOLUTION;
REPEATED dummy_year / subject = plot type = ARH(1);
After looking through other similar questions on here, I'm pretty sure I'm able to re-create the repeated statement in R, including the covariance structure, using the nlme library:
library(nlme)
cover.data <- read.csv("https://drive.google.com/uc?export=donload&id=0Bxdatltmq5ljMVlObXh1NHFGck0", header = TRUE)
cover.data <- cover.data[cover.data$Flag=="data",]
cover.data$soil_grp <- factor(cover.data$soil_grp)
cover.data$dummy_year <- factor(cover.data$dummy_year)
cover.data$pcp <- as.numeric(as.character(cover.data$pcp))
cover.data$cover <- as.numeric(as.character(cover.data$cover))
model.test1 <- gls(cover ~ Pcp + soil_grp, corr = corAR1(, form = ~ 1 | year/plot), weights = varIdent(form = ~ 1 | dummy_year), data = cover.data, na.action = "na.omit")
but I can't figure out how to add in the random effect (with a different covariance structure) into this model.
Additionally, I can put both the repeated and random variables into the same model but don't know how to specify the covariance structures for them.
model.test2 <- lme(cover ~ Pcp_jan_jun + soil, random = list( plot = ~ 1, pasture = ~1), data = cover.data, na.action = "na.omit")
Is is possible to put both a repeated and random effect with different covariance structures into the same model in R? If so, how do I code R to do it?
The data can be downloaded from
https://drive.google.com/uc?export=donload&id=0Bxdatltmq5ljMVlObXh1NHFGck0

Resources