Probing interactions in nlme using the "interactions" package in R - r

I am running a linear mixed effects models using the "nlme" package looking at stress and lifestyle as predictors of change in cognition over 4 years in a longitudinal dataset. All variables in the model are continuous variables.
I am able to create the model and get the summary statistics using this code:
mod1 <- lme(MS ~ age + sex + edu + GDST1*Time + HLI*Time + GDST1*HLI*Time, random= ~ 1|ID, data=NuAge_long, na.action=na.omit)
summary(mod1)
I am trying to use the "interactions" package to probe the 3-way interaction:
sim_slopes(model = mod1, pred = Time, modx = GDST1, mod2 = HLI, data = NuAge_long)
but am receiving this error:
Error in if (tcol == "df") tcol <- "t val." : argument is of length zero
I am also trying to plot the interaction using the same "interactions" package:
interact_plot(model = mod1, pred = Time, modx = GDST1, mod2 = HLI, data = NuAge_long)
and am receiving this error:
Error in UseMethod("family") : no applicable method for 'family' applied to an object of class "lme"
I can't seem to find what these errors mean and why I'm getting them. Any help would be appreciated!

From ?interactions::sim_slopes:
The function is tested with ‘lm’, ‘glm’,
‘svyglm’, ‘merMod’, ‘rq’, ‘brmsfit’, ‘stanreg’ models. Models
from other classes may work as well but are not officially
supported. The model should include the interaction of
interest.
Note this does not include lme models. On the other hand, merMod models are those generated by lme4::[g]lmer(), and as far as I can tell you should be able to fit this model equally well with lmer():
library(lme4)
mod1 <- lmer(MS ~ age + sex + edu + GDST1*Time + HLI*Time + GDST1*HLI*Time
+ (1|ID), data=NuAge_long)
(things will get harder if you want to specify correlation structures, e.g. correlation = corAR1(), which works for lme() but not lmer() ...)

Related

R: How to obtain diagnostic plots for a lavaan mediation model?

I wasn't sure whether this was more appropriate to ask here or CrossValidated as I'm specifically asking about using R / lavaan...
I'm not sure if I've completely misunderstood how violations of assumptions are checked. I understand that we can obtain diagnostic plots for linear models with:
model <- lm(data$outcome ~ data$predictor)
plot(model, which = c(1:6))
But I'm having trouble figuring out how I should do this for a mediation model fitted like so:
model <- 'outcome ~ c*predictor + b*mediator
mediator ~ a*predictor
indirect_effect := a*b
total_effect := c + (a*b)
'
model.fit <- lavaan::sem(
model = model,
data = data,
missing = "FIML",
estimator = "ML")
Then if I try obtaining plots in the same way (plot(model.fit, which = c(1:6))), I get Error in as.double(y) : cannot coerce type 'S4' to vector of type 'double'.
Also, to check for violations of assumptions for Pearson's correlation, would we do so by looking at the structure of each variable individually, or by making a linear model (lm(data$outcome ~ data$predictor)), or using the correlation itself (cor.test(data$var1, data$var2)) in some way?
Try:
lavaanPlot::lavaanPlot(model = model.fit, coefs=T)

Error running betar distriubtion in GAM mgcv

I'm trying to run a gam using the mgcv package with a response variable which is proportional data. The data is overdispered so initially I used a quasibinomial distribution. However because I'm using model selection that's not particularly useful as it does not produce AIC scores.
Instead I'm trying to use betar distribution, as I've read that it could be appropriate.
mRI_br <- bam(ri ~ SE_score + s(deg, k=7) + s(gs, k=7) + TL + species + sex + season + year + s(code, bs = 're') + s(station, bs = 're'), family=betar(), data=node_dat, na.action = "na.fail")
However I'm getting this warnings when I run the model.
Warning messages:
1: In estimate.theta(theta, family, y, mu, scale = scale1, ... :
step failure in theta estimation
And when I try and check the model summary I get this error.
> summary(mRI_br)
Error in chol.default(diag(p) + crossprod(S2 %*% t(R2))) :
the leading minor of order 62 is not positive definite
I would like to know:
What is causing these errors and warnings, and how can they be solved?
If not are there any other distributions that can be used with proportion data which enable me to subsequently use model selection techniques (such as the dredge function from the MuMIn package.
A copy of the dataset can be found here

Goodness of Fit statistic Tobit model

I have estimated a Tobit model using the censReg package, along with the censReg function. Alternatively, the same Tobit model is estimated using the tobit function in the AER package.
Now, I really like to have some goodness of fit statistic, such as the Pseudo-R2. However, whenever I try to estimate this, the output returns as NA. For example:
Tobit <- censReg(Listing$occupancy_rate ~ ., left = -Inf, right = 1, data = Listing)
PseudoR2(Tobit, which = "McFadden")
[1] NA
So far, I have only seen reported Pseudo-R2's when people use Stata. Does anyone know how to estimate it in R?
Alternatively, Tobit estimates the (log)Sigma, which is basically the standard deviation of the residuals. Could I use this to calculate the R2?
All help is really appreciated.
You can use DescTools package to calculate PseudoR2. You have not provided any sample data. So, it is hard for me to run your model. I am using a default dataset like
library(DescTools)
r.glm <- glm(Survived ~ ., data=Untable(Titanic), family=binomial)
PseudoR2(r.glm, c("McFadden"))
For your model, you can use something like
library(AER)
data("Affairs", package = "AER")
fm.tobit <- tobit(affairs ~ age + yearsmarried + religiousness + occupation + rating,
data = Affairs)
#Create a function for pseudoR2 calculation
pseudoR2 <- function(obj) 1 - as.vector(logLik(obj)/logLik(update(obj, . ~ 1)))
pseudoR2(fm.tobit)
#>[1] 0.05258401
Or using censReg as you have used
library(censReg)
data("Affairs", package = "AER")
estResult <- censReg(affairs ~ age + yearsmarried + religiousness +
occupation + rating, data = Affairs)
summary(estResult)
pseudoR2(estResult)
#>[1] 0.05258401
You can find the details about pseudoR2 in the following link
R squared in logistic regression

pglm fixed effect Poisson model with offset

I would like to run a fixed effect Poisson model with panel data in R, with a count variable as the outcome, and the log of the population as an offset variable (i.e. modeling a rate). However, using the example dataset below, I get the same results when I run the two models m1 and m2. I'd be grateful if anyone could point out what I'm doing wrong in terms of specifying m1, or offer a solution using a different package? Many thanks
library(AER)
data(Fatalities)
library(pglm)
m1 <- pglm(fatal ~ beertax + as.factor(year) + offset(log(pop)), index = c("state"), model = "within", effect="individual", data = Fatalities, family = poisson)
summary(m1)
m2 <- pglm(fatal ~ beertax + as.factor(year), index = c("state"), model = "within", effect="individual", data = Fatalities, family = poisson)
summary(m2)
One direct solution is by using glm instead, with dummy variables for year and state:
fit_model <- glm(fatal ~ beertax + as.factor(year) + as.factor(state) + offset(log(pop)) , data = Fatalities, family = poisson)
which gives the same result in STATA (at least using this command: xtpoisson fatal beertax year1-year7, fe offset(log_pop)).
This approach is not feasible when the number of states is reasonably large. In CRAN, there is the novel fixest package (https://cran.r-project.org/web/packages/fixest/index.html) that provides a fast solution with robust standard errors.

Multilevel moderated mediation with continuous variables

I am a beginner in R, so please forgive me if my question reflects insufficient background.
I am trying to run a moderated mediation model using the mediation and lme4 libraries.
All of my variables are continuous. My data have a nested structure with individuals nested in branches (Branch).
In the model I'm trying to test, my predictor/independent variable (abranch) is at the branch level. My mediator (bmed) and outcome (cout) are at the individual level. And the effect of the mediator is moderated by another individual level variable (dmod). So in my model I have abranch predicting bmed, and bmed*dmod are predicting cout.
This is the syntax I've used:
med.fit <- glmer(
bmed ~ abranch + (1|Branch),
family = binomial(link = "logit"),
data = Dataset
)
out.fit <- glmer(
cout ~ dmod*bmed + (1+bmed|Branch),
family = binomial(link = "logit"),
data = Dataset
)
I was then thinking of using:
med.out <- mediate(med.fit, out.fit, treat = "abranch", mediator = "bmed",
+ sims = 100)
summary(med.out)
But even before getting to the last two lines, I get the following error:
Error in eval(family$initialize, rho) : y values must be 0 <= y <= 1
I now realize that this is because I'm using the "binomial"/logit family whereas my DV is continuous and not between 0 and 1. What can I do, given the nature of my variables?

Resources