Interaction plot for lme4 in r - r

I would like to plot an interaction effect for two continuous variables in a multilevel model. The model is the following:
lme4::glmer(final$trust ~ final$educ.c +
final$age.c +
final$gender +
final$C14 RESPONDENT_OCCUPATION_SCALE +
final$tot_country.c +
final$int_use.c +
final$educ.c*final$acc.c +
(final$educ.c | final$Country),
data = final,
family = binomial,
weights = final$W85_WEIGHT_EU27_+_TR_+_HR_+_NO_+_CH_+_IS, nAGQ=0,
control = glmerControl(optimizer = "optimx", calc.derivs = FALSE, optCtrl = list(method = "nlminb", starttests = FALSE, kkt = FALSE)))
The interaction I am interested in is final$educ.c*final$acc.c, the dependent variable is dichotomous and after have consulted various websites and similar issues, I am still struggling with it. Does anyone have a suggestion about how to do it?

Related

Cross Validation Structural Equation Modeling

Not sure why it is difficult to find info on this topic.
I want to CV my SEM model. N = 360. I've pulled 70% of data into a train set and have built the model, first on theory then using modification indices. I also have a test data frame where I have the observed values (for well-being), but I want to use the model to predict the values. lavPredict only seems to be used to predict values of latent variables. Perhaps I'm missing something, but doesn't seem so straightforward as in lmer or basic linear regression. Does one just use the model fit indices from the test dataset? Seems like one should be able to compare observed and predicted values in SEM.
I've included some data here: https://drive.google.com/file/d/1AX50DFNik30Qsyiyp6XnPMETNfVXK83r/view?usp=sharing
Here is the final model I have through the train dataset. When I go to test it, I just get this
Error in lavPredict(fit.latent.8, newdata = test) :
inherits(object, "lavaan") is not TRUE
Thanks much!
fit.latent.8 <- '#factor loadings; measurement model portion
pl =~ exercisescore + mindfulnessscore + promistscore
sl =~ family_support + friendshipcount + friendshipnet + sense_of_community
trauma =~ neglectscore + abusescore + exposure + family_support + age + sesscore
#regressions: structural model
wellbeing ~ age + gender + ethnicity + sesscore + resiliencescore + pl + emotionalsupportscore + trauma
resiliencescore ~ age + sesscore + emotionalsupportscore + pl
emotionalsupportscore ~ sl + gender
#Covariances
friendshipnet~~age
friendshipnet ~~ abusescore
'
train.1 <- sem(fit.latent.8, data = train, meanstructure = TRUE, std.lv = TRUE)
summary(train.1, fit.measures = TRUE,standardized = TRUE, rsquare = TRUE, estimates = FALSE)
modindices(train.1, sort. = TRUE, minimum.value = 10)
test.1 <- sem(fit.latent.8, data = test, meanstructure = TRUE, std.lv = TRUE)
summary(test.1, fit.measures = TRUE,standardized = TRUE, rsquare = TRUE, estimates = FALSE)

Fitting two coefplot in one graph using par(mfrow()) method

I'm trying to arrange two coefplot objects into one graph via the par(mfrow(,)) method, but it didn't work out. What did I do wrong? Or is that coefplot just doesn't work this way? What will be alternative method?
I've referenced this earlier thread, but I tend to think that mine is a quite different issue.
# load the data
dat <- readRDS(url("https://www.dropbox.com/s/88h7hmiroalx3de/act.rds?dl=1"))
#fit two models
library(lmer4)
act1.fit <- glmer(act1 ~ os + education + marital + nat6 + nat5 + nat4 + nat3 + nat2 + nat1 +
(1 | region_id), data = action, family = binomial, control = glmerControl(optimizer = "bobyqa"),
nAGQ = 10)
action2.fit <- glmer(act2 ~ os + education + marital + nat6 + nat5 + nat4 + nat3 + nat2 + nat1 +
(1 | region_id), data = action, family = binomial, control = glmerControl(optimizer = "bobyqa"),
nAGQ = 10)
# plot the two model individually
library(coefplot)
# construct coefplot objects
coefplot:::buildModelCI(action1.fit)
coefplot:::buildModelCI(action2.fit)
coefplot(action2.fit, coefficients=c("nat1", "nat2", "nat3", "nat4", "nat5", "nat6"),
intercept = FALSE, color = "brown3")
# arrange two plots in one graph
par(mfrow=c(1,2))
coefplot(action1.fit, coefficients=c("nat1", "nat2", "nat3", "nat4", "nat5", "nat6"),
intercept = FALSE, color = "brown3")
coefplot(action2.fit, coefficients=c("nat1", "nat2", "nat3", "nat4", "nat5", "nat6"),
intercept = FALSE, color = "brown3")
# didn't work ???

R splm: Spatial panel with no exogenous regressors

I want to estimate the spatial panel autoregressive model
y_{t} = a + \rho W y_{t} + \epsilon_{t}
where a is a vector of individual fixed effects. I am using the excellent splm package in R.
Note that I don't have any independent variables X here - if I include some regressors X there is no problem, but I wonder how to specify the model with splm in the absence of independent variables.
library(splm)
library("spdep")
data("Produc", package = "Ecdat")
data("usaww")
usalw <- mat2listw(usaww)
# this works well since I have independent regressors
spml(formula = log(gsp) ~ log(pcap), data = Produc,
listw = usaww, lag = TRUE, spatial.error = "none", model = "within",
effect = "twoways")
# this does not work
spml(formula = log(gsp) ~ ., data = Produc,
listw = usaww, lag = TRUE, spatial.error = "none",
model = "within", effect = "individual")
To estimate an "empty" model (intercept only) the formula has to be y ~ 1. This currently works with random or no individual effects, "within" (fixed effects) estimators need a fix.
A workaround for getting the FE estimates: explicit demeaning of the data
library(plm)
spml(formula = Within(log(gsp)) ~ 1, data = Produc,
listw = usaww, lag = TRUE, spatial.error = "none",
model = "pooling")

ggplot GLM fitted curve without interaction

I want to add the fitted function from GLM on a ggplot. By default, it automatically create the plot with interaction. I am wondering, if I can plot the fitted function from the model without interaction. For example,
dta <- read.csv("http://www.ats.ucla.edu/stat/data/poisson_sim.csv")
dta <- within(dta, {
prog <- factor(prog, levels=1:3, labels=c("General", "Academic", "Vocational"))
id <- factor(id)
})
plt <- ggplot(dta, aes(math, num_awards, col = prog)) +
geom_point(size = 2) +
geom_smooth(method = "glm", , se = F,
method.args = list(family = "poisson"))
print(plt)
gives the plot with interaction,
However, I want the plot from the model,
`num_awards` = ß0 + ß1*`math` + ß2*`prog` + error
I tried to get this this way,
mod <- glm(num_awards ~ math + prog, data = dta, family = "poisson")
fun.gen <- function(awd) exp(mod$coef[1] + mod$coef[2] * awd)
fun.acd <- function(awd) exp(mod$coef[1] + mod$coef[2] * awd + mod$coef[3])
fun.voc <- function(awd) exp(mod$coef[1] + mod$coef[2] * awd + mod$coef[4])
ggplot(dta, aes(math, num_awards, col = prog)) +
geom_point() +
stat_function(fun = fun.gen, col = "red") +
stat_function(fun = fun.acd, col = "green") +
stat_function(fun = fun.voc, col = "blue") +
geom_smooth(method = "glm", se = F,
method.args = list(family = "poisson"), linetype = "dashed")
The output plot is
Is there any simple way in ggplot to do this efficiently?
Ben's idea of plotting predicted value of the response for specific model terms inspired me improving the type = "y.pc" option of the sjp.glm function. A new update is on GitHub, with version number 1.9.4-3.
Now you can plot predicted values for specific terms, one which is used along the x-axis, and a second one used as grouping factor:
sjp.glm(mod, type = "y.pc", vars = c("math", "prog"))
which gives you following plot:
The vars argument is needed in case your model has more than two terms, to specify the term for the x-axis-range and the term for the grouping.
You can also facet the groups:
sjp.glm(mod, type = "y.pc", vars = c("math", "prog"), show.ci = T, facet.grid = T)
There's no way that I know of to trick geom_smooth() into doing this, but you can do a little better than you've done. You still have to fit the model yourself and add the lines, but you can use the predict() method to generate the predictions and load them into a data frame with the same structure as the original data ...
mod <- glm(num_awards ~ math + prog, data = dta, family = "poisson")
## generate prediction frame
pframe <- with(dta,
expand.grid(math=seq(min(math),max(math),length=51),
prog=levels(prog)))
## add predicted values (on response scale) to prediction frame
pframe$num_awards <- predict(mod,newdata=pframe,type="response")
ggplot(dta, aes(math, num_awards, col = prog)) +
geom_point() +
geom_smooth(method = "glm", se = FALSE,
method.args = list(family = "poisson"), linetype = "dashed")+
geom_line(data=pframe) ## use prediction data here
## (inherits aesthetics etc. from main ggplot call)
(the only difference here is that the way I've done it the predictions span the full horizontal range for all groups, as if you had specified fullrange=TRUE in geom_smooth()).
In principle it seems as though the sjPlot package should be able to handle this sort of thing, but it looks like the relevant bit of code for doing this plot type is hard-coded to assume a binomial GLM ... oh well.
I'm not sure, but you wrote "without interaction" - maybe you are looking for effect plots? (If not, excuse me that I'm assuming something completely wrong...)
You can, for instance, use the effects package for this.
dta <- read.csv("http://www.ats.ucla.edu/stat/data/poisson_sim.csv")
dta <- within(dta, {
prog <- factor(prog, levels=1:3, labels=c("General", "Academic", "Vocational"))
id <- factor(id)
})
mod <- glm(num_awards ~ math + prog, data = dta, family = "poisson")
library(effects)
plot(allEffects(mod))
Another option would be the sjPlot package, as Ben suggested - however, the current version on CRAN only supports logistic regression models properly for effect plots. But in the current development version on GitHub I added support for various model families and link functions, so if you like, you can download that snapshot. The sjPlot package uses ggplot instead of lattice (which is used by the effects package, I think):
sjp.glm(mod, type = "eff", show.ci = T)
Or in non-faceted way:
sjp.glm(mod, type = "eff", facet.grid = F, show.ci = T)

How do you estimate the scale parameter for a random parameter logit using mlogit in R

I am trying to estimate a random parameter logit/mixed logit model using the mlogit package in R. My model looks like this,
wlsq.obese_conf.model <- mlogit(formula = choice ~ noadveff + tab + infreq_1 + weightloss + I(weightloss^2) + optout + cost| -1 | 0,
data = mlogit.obese_conf,
rpar = c(noadveff = 'n', tab = 'n', infreq_1 = 'n', weightloss = 'n', optout1 = "n"),
R = 100,
halton = NA,
print.level = 0,
panel = TRUE,
estimate = TRUE,
weights = weighting.x
)
I am running the same model on a different subsample, and need to compare the scale parameters between the two subsamples. However, I have not been able to identify a method for extracting the parameter - do anyone know how I can do this in mlogit?

Resources