standard error of outcome in lm and lme - r

I have the following linear models
library(nlme)
fm2 <- lme(distance ~ age + Sex, data = Orthodont, random = ~ 1)
fm2.lm <- lm(distance ~ age + Sex,data = Orthodont)
How can I obtain the standard error of distance with age and Sex?

For fm2 (linear mixed model), you can do
sqrt(diag(summary(fm2)$varFix))
#(Intercept) age SexFemale
# 0.83392247 0.06160592 0.76141685
For fm2.lm (linear model), you can do
summary(fm2.lm)$coefficients[, "Std. Error"]
#(Intercept) age SexFemale
# 1.11220946 0.09775895 0.44488623

see attributes(summary(your.model)). what you're after is summary(your.model)$coefficients (or did I get your question wrong?). just use subsetting with [] to get what you want

Related

Extracting linear predictors from general linear model for panel data ( pglm )

I would like to extract linear predictors out of a pglm model.
With a basic glm model, it can be done simply using the command $linear.predictors
#For example
library(plm)
library(pglm)
data(UnionWage) # from pglm-package
punions <- pdata.frame(UnionWage, c("id", "year"))
punions <- subset(punions, wage > 0)
glm.model <- glm(wage ~ exper + rural + married, data=punions, family = "poisson")
glm.model$linear.predictors
How can I extract or compute linear predictors from a pglm model like
pglm.model <- pglm(wage ~ exper + rural + married, data=punions, model="random", family="poisson")
I hope anybody can help!

Treatment as a fixed effect in a non-linear mixed model

I would like to fit a nonlinear mixed model and then test differences between parameters in treatment and control groups.
I am using nlmer from the lme4 package.
I am using the Oranges dataset as test data for this problem.
The circumferences of 5 trees are measured over time. Each tree exhibits logistic growth. In the basic example, we include Tree as a random effect.
I have extended the data so that there is a treat and control group (the treat is just a copy of control with circumference values doubled).
My problem is, I'd like to have 'treat' as a fixed effect and then test the differences between the non-linear model parameter Asym in the treatment and control groups.
library(lme4)
#Toy data based on Orange (lme4)
# Create a copy of Orange data, double the circumference values, make new labels for trees (no. 6-10) and label all as treatment (1)
Orange.with.treatment<-Orange
Orange.with.treatment$circumference<-Orange.with.treatment$circumference*2
Orange.with.treatment$Tree <- as.factor(as.numeric(Orange.with.treatment$Tree) + 5)
Orange.with.treatment$treat<- as.factor(rep(1,length(Orange$Tree)))
# Create a copy of Orange data and label all as control (1)
Orange.control<-Orange
Orange.control$treat<- as.factor(rep(0,length(Orange$Tree)))
# combine into one dataframe
Orange.full<-(rbind(Orange.control,Orange.with.treatment))
# a nlmer fit not considering treatment as a factor
startvec <- c(Asym = 200, xmid = 725, scal = 350)
(nm1 <- nlmer(circumference ~ SSlogis(age, Asym, xmid, scal) ~ Asym|Tree,
Orange.full, start = startvec))
# a nlmer fit considering treatment as a fixed factor?
startvec <- c(Asym = 200, xmid = 725, scal = 350)
(nm2 <- nlmer(circumference ~ SSlogis(age, Asym, xmid, scal) ~ Asym+treat|Tree,
Orange.full, start = startvec))
# test differences in parameters between treat and control?
I have tried adding treat alongside Asym in the formula, but I don;t think that is correct.
What I would like is a summary of Asym in treat and control, and a way to statistically test the difference between them.
Since you seem to be open to using other tools, here is an nlme solution:
library(nlme)
mod <- nlme(circumference ~ SSlogis(age, Asym, xmid, scal), data = Orange.full,
fixed = Asym + xmid + scal ~ treat, random = Asym + xmid + scal ~ 1 | Tree,
start = c(200, 200, 725, 0, 350, 0), control = nlmeControl(msMaxIter = 1000))
summary(mod)
#Nonlinear mixed-effects model fit by maximum likelihood
# Model: circumference ~ SSlogis(age, Asym, xmid, scal)
# Data: Orange.full
# AIC BIC logLik
# 608.9452 638.1756 -291.4726
#
#Random effects:
# Formula: list(Asym ~ 1, xmid ~ 1, scal ~ 1)
# Level: Tree
# Structure: General positive-definite, Log-Cholesky parametrization
# StdDev Corr
#Asym.(Intercept) 43.23426 As.(I) xm.(I)
#xmid.(Intercept) 38.35359 -0.031
#scal.(Intercept) 32.49873 -0.968 0.279
#Residual 11.27260
#
#Fixed effects: Asym + xmid + scal ~ treat
# Value Std.Error DF t-value p-value
#Asym.(Intercept) 191.2135 22.30629 55 8.572177 0.0000
#Asym.treat1 193.0409 31.56922 55 6.114847 0.0000
#xmid.(Intercept) 722.4272 53.37976 55 13.533729 0.0000
#xmid.treat1 5.0466 62.02158 55 0.081368 0.9354
#scal.(Intercept) 349.4497 41.68009 55 8.384092 0.0000
#scal.treat1 7.3181 48.41709 55 0.151146 0.8804
#
#<snip>
As you see, this shows a significant treatment effect on the asymptote but not on the other parameters, as expected.

how to config nlme to give the same results as lme

The question looks naive but I am puzzled with the configuration of the nlme function in R to get equivalent results to a given lme model.
This appears to work. Note that the defaults for method are different for lme ("REML") and nlme ("ML") ...
m1 <- lme(distance ~ age,
random = ~ age |Subject, data=Orthodont,
method="ML")
nlme requires starting values - cheat here and use those from lme:
m2 <- nlme(distance ~ mu,
fixed = mu ~ age,
random = mu ~ age | Subject,
data=Orthodont,
start=list(fixed=fixef(m1)))
The variance-covariance matrices are almost identical.
> VarCorr(m1)
Subject = pdLogChol(age)
Variance StdDev Corr
(Intercept) 4.81407327 2.1940996 (Intr)
age 0.04619252 0.2149244 -0.581
Residual 1.71620466 1.3100399
> VarCorr(m2)
Subject = pdLogChol(list(mu ~ age))
Variance StdDev Corr
mu.(Intercept) 4.81408901 2.1941032 m.(In)
mu.age 0.04619255 0.2149245 -0.581
Residual 1.71620373 1.3100396

How to add a random intercept and random slope term to a GAMM model in R

I am trying to specify both a random intercept and random slope term in a GAMM model with one fixed effect.
I have successfully fitted a model with a random intercept using the below code within the mgcv library, but can now not determine what the syntax is for a random slope within the gamm() function:
M1 = gamm(dur ~ s(dep, bs="ts", k = 4), random= list(fInd = ~1), data= df)
If I was using both a random intercept and slope within a linear mixed-effects model I would write it in the following way:
M2 = lme(dur ~ dep, random=~1 + dep|fInd, data=df)
The gamm() supporting documentation states that the random terms need to be given in the list form as in lme() but I cannot find any interpretable examples that include both slope and intercept terms. Any advice / solutions would be much appreciated.
The gamm4 function in the gamm4 package contains a way to do this. You specify the random intercept and slope in the same way that you do in the lmer style. In your case:
M1 = gamm4(dur~s(dep,bs="ts",k=4), random = ~(1+dep|fInd), data=df)
Here is the gamm4 documentation:
https://cran.r-project.org/web/packages/gamm4/gamm4.pdf
Here is the gamm() syntax to enter correlated random intercept and slope effects, using the sleepstudy dataset.
library(nlme)
library(mgcv)
data(sleepstudy,package='lme4')
# Model via lme()
fm1 <- lme(Reaction ~ Days, random= ~1+Days|Subject, data=sleepstudy, method='REML')
# Model via gamm()
fm1.gamm <- gamm(Reaction ~ Days, random= list(Subject=~1+Days), data=sleepstudy, method='REML')
VarCorr(fm1)
VarCorr(fm1.gamm$lme)
# Both are identical
# Subject = pdLogChol(1 + Days)
# Variance StdDev Corr
# (Intercept) 612.0795 24.740241 (Intr)
# Days 35.0713 5.922103 0.066
# Residual 654.9424 25.591843
The syntax to enter uncorrelated random intercept and slope effects is the same for lme() and gamm().
# Model via lme()
fm2 <- lme(Reaction ~ Days, random= list(Subject=~1, Subject=~0+Days), data=sleepstudy, method='REML')
# Model via gamm()
fm2.gamm <- gamm(Reaction ~ Days, random= list(Subject=~1, Subject=~0+Days), data=sleepstudy, method='REML')
VarCorr(fm2)
VarCorr(fm2.gamm$lme)
# Both are identical
# Variance StdDev
# Subject = pdLogChol(1)
# (Intercept) 627.5690 25.051328
# Subject = pdLogChol(0 + Days)
# Days 35.8582 5.988172
# Residual 653.5838 25.565285
This answer also shows how to enter multiple random effects into lme().

Likelihood Ratio Test for Cox Models in R

Does anyone know of a likelihood ratio test, like lrtest in the lmtest package, that works for cox proportional hazards models produced using coxph? lrtest does not seem to work for coxph models.
Thanks
There is an anova.coxph in pkg:survival which allows comparison of model objects.
fit <- coxph(Surv(futime, fustat) ~ resid.ds *rx + ecog.ps, data = ovarian)
fit2 <- coxph(Surv(futime, fustat) ~ resid.ds +rx + ecog.ps, data=ovarian)
anova(fit2,fit)
Analysis of Deviance Table
Cox model: response is Surv(futime, fustat)
Model 1: ~ resid.ds + rx + ecog.ps
Model 2: ~ resid.ds * rx + ecog.ps
loglik Chisq Df P(>|Chi|)
1 -31.970
2 -30.946 2.0469 1 0.1525
This is an LR test.
w.r.t. the comment. A "null model" in Cox regression would be formed with only a 1 on the RHS of the formula-tilde:
fit <- coxph(Surv(futime, fustat) ~ 1, data = ovarian)
LR-Test is returned by default by coxph() from thr survival package (see last line):
require(survival)
test1 <- list(time=c(4,3,1,1,2,2,3),
status=c(1,1,1,0,1,1,0),
x=c(0,2,1,1,1,0,0),
sex=c(0,0,0,0,1,1,1))
# Fit a stratified model
coxph(Surv(time, status) ~ x + strata(sex), test1)
Call:
coxph(formula = Surv(time, status) ~ x + strata(sex), data = test1)
coef exp(coef) se(coef) z p
x 0.802 2.23 0.822 0.976 0.33
Likelihood ratio test=1.09 on 1 df, p=0.297 n= 7, number of events= 5

Resources