I am using LME model defined like:
mod4.lme <- lme(pRNFL ~ Init.Age + Status + I(Time^2), random= ~1|Patient/EyeID,data = long1, na.action = na.omit)
The output is:
> summary(mod4.lme)
Linear mixed-effects model fit by REML
Data: long1
AIC BIC logLik
2055.295 2089.432 -1018.647
Random effects:
Formula: ~1 | Patient
(Intercept)
StdDev: 7.949465
Formula: ~1 | EyeID %in% Patient
(Intercept) Residual
StdDev: 12.10405 2.279917
Fixed effects: pRNFL ~ Init.Age + Status + I(Time^2)
Value Std.Error DF t-value p-value
(Intercept) 97.27827 6.156093 212 15.801950 0.0000
Init.Age 0.02114 0.131122 57 0.161261 0.8725
StatusA -27.32643 3.762155 212 -7.263504 0.0000
StatusF -23.31652 3.984353 212 -5.852023 0.0000
StatusN -0.28814 3.744980 57 -0.076940 0.9389
I(Time^2) -0.06498 0.030223 212 -2.149921 0.0327
Correlation:
(Intr) Int.Ag StatsA StatsF StatsN
Init.Age -0.921
StatusA -0.317 0.076
StatusF -0.314 0.088 0.834
StatusN -0.049 -0.216 0.390 0.365
I(Time^2) -0.006 -0.004 0.001 -0.038 -0.007
Standardized Within-Group Residuals:
Min Q1 Med Q3 Max
-2.3565641 -0.4765840 0.0100608 0.4670792 2.7775392
Number of Observations: 334
Number of Groups:
Patient EyeID %in% Patient
60 119
I wanted to get comparisons between my 'Status' factors (named A, N, F and H). So I did a emmeans model using this code:
emmeans(mod4.lme, pairwise ~ Status, adjust="bonferroni")
The output for this, is:
> emmeans(mod4.lme, pairwise ~ Status, adjust="bonferroni")
$emmeans
Status emmean SE df lower.CL upper.CL
H 98.13515 2.402248 57 93.32473 102.94557
A 70.80872 2.930072 57 64.94135 76.67609
F 74.81863 3.215350 57 68.38000 81.25726
N 97.84701 2.829706 57 92.18062 103.51340
Degrees-of-freedom method: containment
Confidence level used: 0.95
$contrasts
contrast estimate SE df t.ratio p.value
H - A 27.3264289 3.762155 212 7.264 <.0001
H - F 23.3165220 3.984353 212 5.852 <.0001
H - N 0.2881375 3.744980 57 0.077 1.0000
A - F -4.0099069 2.242793 212 -1.788 0.4513
A - N -27.0382913 4.145370 57 -6.523 <.0001
F - N -23.0283844 4.359019 57 -5.283 <.0001
The answer is yes, emmeans does the calculation based on the model
Related
I have been trying to convert some PROC MIXED SAS code into R, but without success. The code is:
proc mixed data=rmanova4;
class randomization_arm cancer_type site wk;
model chgpf=randomization_arm cancer_type site wk;
repeated / subject=study_id;
contrast '12 vs 4' randomization_arm 1 -1;
lsmeans randomization_arm / cl pdiff alpha=0.05;
run;quit;
I have tried something like
mod4 <- lme(chgpf ~ Randomization_Arm + Cancer_Type + site + wk, data=rmanova.data, random = ~ 1 | Study_ID, na.action=na.exclude)
but I am getting different estimate values.
Perhaps I am misunderstanding something basic. Any comment/suggestion would be greatly appreciated.
(Additional editing)
I am adding here the output. Part of the output from the SAS code is below:
Least Squares Means
Effect Randomization_Arm Estimate Standard Error DF t Value Pr > |t| Alpha Lower Upper
Randomization_Arm 12 weekly BTA -4.5441 1.3163 222 -3.45 0.0007 0.05 -7.1382 -1.9501
Randomization_Arm 4 weekly BTA -6.4224 1.3143 222 -4.89 <.0001 0.05 -9.0126 -3.8322
Differences of Least Squares Means
Effect Randomization_Arm _Randomization_Arm Estimate Standard Error DF t Value Pr > |t| Alpha Lower Upper
Randomization_Arm 12 weekly BTA 4 weekly BTA 1.8783 1.4774 222 1.27 0.2049 0.05 -1.0332 4.7898
The output from the R code is below:
Linear mixed-effects model fit by REML
Data: rmanova.data
AIC BIC logLik
6522.977 6578.592 -3249.488
Random effects:
Formula: ~1 | Study_ID
(Intercept) Residual
StdDev: 16.59143 12.81334
Fixed effects: chgpf ~ Randomization_Arm + Cancer_Type + site + wk
Value Std.Error DF t-value p-value
(Intercept) 2.332268 2.314150 539 1.0078294 0.3140
Randomization_Arm4 weekly BTA -1.708401 2.409444 222 -0.7090435 0.4790
Cancer_TypeProsta -4.793787 2.560133 222 -1.8724761 0.0625
site2 -1.492911 3.665674 222 -0.4072678 0.6842
site3 -4.002252 3.510111 222 -1.1402066 0.2554
site4 -12.013758 5.746988 222 -2.0904442 0.0377
site5 -3.823504 4.938590 222 -0.7742097 0.4396
wk2 0.313863 1.281047 539 0.2450052 0.8065
wk3 -3.606267 1.329357 539 -2.7127905 0.0069
wk4 -4.246526 1.345526 539 -3.1560334 0.0017
Correlation:
(Intr) R_A4wB Cnc_TP site2 site3 site4 site5 wk2 wk3
Randomization_Arm4 weekly BTA -0.558
Cancer_TypeProsta -0.404 0.046
site2 -0.257 0.001 -0.087
site3 -0.238 0.004 -0.163 0.201
site4 -0.255 0.031 0.151 0.101 0.095
site5 -0.172 -0.016 -0.077 0.139 0.151 0.073
wk2 -0.254 -0.008 0.010 0.011 -0.003 0.005 -0.001
wk3 -0.257 0.005 0.020 0.014 0.006 -0.001 -0.002 0.464
wk4 -0.251 -0.007 0.022 0.020 0.002 0.006 -0.002 0.461 0.461
Standardized Within-Group Residuals:
Min Q1 Med Q3 Max
-5.6784364 -0.3796392 0.1050812 0.4588555 3.1055046
Number of Observations: 771
Number of Groups: 229
Adding some comments and observations
Since my original posting, I have tried various pieces of R code but I am getting different estimates from those given in SAS.
More importantly, the standard errors are almost double than those given by SAS.
Any suggestions would be greatly appreciated.
I got the solution to the problem from someone after posting the question at the R-sig-ME. It seems that the above SAS fits actually a simple linear regression model, assuming independent across observations, which is equivalent to
proc glm data=rmanova4;
class randomization_arm cancer_type site wk;
model chgpf = randomization_arm cancer_type site wk;
run;
which of course in R is equivalent to
lm(chgpf ~ Randomization_Arm + Cancer_Type + site + wk, data=rmanova.data)
I made a glmer model to predict correct responses as a function of two independent variables (2x2 within-subjects design).
Here is the head of the df with ID, stimulus, the two within-subj conditions, the dependent variable "correct" and the predicted probability from the glmer fit (added after model computation).
Note: the correct variable was computed from the rate colum (if participants' rate < 49 and cond_A = near it is correct, if rate > 51 and cond_A = far, it is correct; the opposite for incorrect responses)
ID stimulus cond_A cond_B rate correct prob
1 5ef197dadda04d0008ba9dce FIORE near noncongr 0 1 0.5239730
2 5ef197dadda04d0008ba9dce TRENO near noncongr 19 1 0.5443476
3 5ef197dadda04d0008ba9dce MESE far noncongr 9 0 0.6530908
4 5ef197dadda04d0008ba9dce MONDO far noncongr 28 0 0.7131941
5 5ef197dadda04d0008ba9dce VASO near noncongr 63 0 0.4607776
6 5ef197dadda04d0008ba9dce SEGNO far congr 7 0 0.6626701
the results of the anova are below
fit = glmer(correct ~ spazio*linguag + (1|ID) + (1|stimulus), family = "binomial", data = correct)
car::Anova(fit, type = 3)
Analysis of Deviance Table (Type III Wald chisquare tests)
Response: correct
Chisq Df Pr(>Chisq)
(Intercept) 24.6270 1 6.957e-07 ***
spazio 0.2052 1 0.6505643
linguag 1.2483 1 0.2638814
spazio:linguag 14.1912 1 0.0001651 ***
now I need to explore among which levels the interaction is significant.
Is the emmean code correct? (why df is Inf?)
Any suggestions for improving and/or plotting the post hoc?
Thanks!
post = emmeans(fit, pairwise~linguag|spazio)
$emmeans
spazio = far:
linguag emmean SE df asymp.LCL asymp.UCL
congr 0.6050 0.122 Inf 0.332 0.878
noncongr 0.8009 0.129 Inf 0.512 1.090
spazio = near:
linguag emmean SE df asymp.LCL asymp.UCL
congr 0.5396 0.121 Inf 0.268 0.811
noncongr -0.0529 0.123 Inf -0.328 0.222
Results are given on the logit (not the response) scale.
Confidence level used: 0.95
Conf-level adjustment: bonferroni method for 2 estimates
$contrasts
spazio = far:
contrast estimate SE df z.ratio p.value
congr - noncongr -0.196 0.175 Inf -1.117 0.2639
spazio = near:
contrast estimate SE df z.ratio p.value
congr - noncongr 0.593 0.170 Inf 3.478 0.0005
Results are given on the log odds ratio (not the response) scale.
I have a 2x2 factorial design: control vs enriched, and strain1 vs strain2. I wanted to make a linear model, which I did as follows:
anova(lmer(length ~ Strain + Insect + Strain:Insect + BW_final + (1|Pen), data = mydata))
Where length is one of the dependent variables I want to analyse, Strain and Insect as treatments, Strain:Insect as interaction effect, BW_final as covariate, and Pen as random effect.
As output I get this:
Sum Sq Mean Sq NumDF DenDF F value Pr(>F)
Strain 3.274 3.274 1 65 0.1215 0.7285
Insect 14.452 14.452 1 65 0.5365 0.4665
BW_final 45.143 45.143 1 65 1.6757 0.2001
Strain:Insect 52.813 52.813 1 65 1.9604 0.1662
As you can see, I only get 1 interaction term: Strain:Insect. However, I'd like to see 4 interaction terms: Strain1:Control, Strain1:Enriched, Strain2:Control, Strain2:Enriched.
Is there any way to do this in R?
Using summary instead of anova I get:
> summary(linearmer)
Linear mixed model fit by REML. t-tests use Satterthwaite's method [lmerModLmerTest]
Formula: length ~ Strain + Insect + Strain:Insect + BW_final + (1 | Pen)
Data: mydata_young
REML criterion at convergence: 424.2
Scaled residuals:
Min 1Q Median 3Q Max
-1.95735 -0.52107 0.07014 0.43928 2.13383
Random effects:
Groups Name Variance Std.Dev.
Pen (Intercept) 0.00 0.00
Residual 26.94 5.19
Number of obs: 70, groups: Pen, 27
Fixed effects:
Estimate Std. Error df t value Pr(>|t|)
(Intercept) 101.646129 7.530496 65.000000 13.498 <2e-16 ***
StrainRoss 0.648688 1.860745 65.000000 0.349 0.729
Insect 0.822454 2.062696 65.000000 0.399 0.691
BW_final -0.005188 0.004008 65.000000 -1.294 0.200
StrainRoss:Insect -3.608430 2.577182 65.000000 -1.400 0.166
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Correlation of Fixed Effects:
(Intr) StrnRs Insect BW_fnl
StrainRoss 0.253
Insect -0.275 0.375
BW_final -0.985 -0.378 0.169
StrnRss:Ins 0.071 -0.625 -0.775 0.016
convergence code: 0
boundary (singular) fit: see ?isSingular```
i'm new to glmmPQL.
library(MASS)
pql<-glmmPQL(fixed = sleeve~pain+stiff+diff,random = ~1|time,family = "binomial",data = knee)
when i run this model R give this warning "iteration 1", i don't know what it means
summary(pql)
Data: knee
AIC BIC logLik
NA NA NA
what it means by NA in AIC,BIC and loglik?
full result look like this
Linear mixed-effects model fit by maximum likelihood
Data: knee
AIC BIC logLik
NA NA NA
Random effects:
Formula: ~1 | time
(Intercept) Residual
StdDev: 4.93377e-05 0.9880118
Variance function:
Structure: fixed weights
Formula: ~invwt
Fixed effects: sleeve ~ pain + stiff + diff
Value Std.Error DF t-value p-value
(Intercept) 0.5775110 0.6854296 21 0.8425534 0.4090
pain -0.0844256 0.1856925 21 -0.4546528 0.6540
stiff -0.0152056 0.1430767 21 -0.1062757 0.9164
diff 0.0022752 0.0415744 21 0.0547254 0.9569
Correlation:
(Intr) pain stiff
pain -0.004
stiff -0.275 -0.369
diff -0.134 -0.901 0.058
Standardized Within-Group Residuals:
Min Q1 Med Q3 Max
-1.3181996 -0.8774770 -0.4111929 0.8923366 1.7722900
Number of Observations: 27
Number of Groups: 3
I am using LME model defined like:
mod4.lme <- lme(pRNFL ~ Init.Age + Status + I(Time^2), random= ~1|Patient/EyeID,data = long1, na.action = na.omit)
The output is:
> summary(mod4.lme)
Linear mixed-effects model fit by REML
Data: long1
AIC BIC logLik
2055.295 2089.432 -1018.647
Random effects:
Formula: ~1 | Patient
(Intercept)
StdDev: 7.949465
Formula: ~1 | EyeID %in% Patient
(Intercept) Residual
StdDev: 12.10405 2.279917
Fixed effects: pRNFL ~ Init.Age + Status + I(Time^2)
Value Std.Error DF t-value p-value
(Intercept) 97.27827 6.156093 212 15.801950 0.0000
Init.Age 0.02114 0.131122 57 0.161261 0.8725
StatusA -27.32643 3.762155 212 -7.263504 0.0000
StatusF -23.31652 3.984353 212 -5.852023 0.0000
StatusN -0.28814 3.744980 57 -0.076940 0.9389
I(Time^2) -0.06498 0.030223 212 -2.149921 0.0327
Correlation:
(Intr) Int.Ag StatsA StatsF StatsN
Init.Age -0.921
StatusA -0.317 0.076
StatusF -0.314 0.088 0.834
StatusN -0.049 -0.216 0.390 0.365
I(Time^2) -0.006 -0.004 0.001 -0.038 -0.007
Standardized Within-Group Residuals:
Min Q1 Med Q3 Max
-2.3565641 -0.4765840 0.0100608 0.4670792 2.7775392
Number of Observations: 334
Number of Groups:
Patient EyeID %in% Patient
60 119
I wanted to get comparisons between my 'Status' factors (named A, N, F and H). So I did a emmeans model using this code:
emmeans(mod4.lme, pairwise ~ Status, adjust="bonferroni")
The output for this, is:
> emmeans(mod4.lme, pairwise ~ Status, adjust="bonferroni")
$emmeans
Status emmean SE df lower.CL upper.CL
H 98.13515 2.402248 57 93.32473 102.94557
A 70.80872 2.930072 57 64.94135 76.67609
F 74.81863 3.215350 57 68.38000 81.25726
N 97.84701 2.829706 57 92.18062 103.51340
Degrees-of-freedom method: containment
Confidence level used: 0.95
$contrasts
contrast estimate SE df t.ratio p.value
H - A 27.3264289 3.762155 212 7.264 <.0001
H - F 23.3165220 3.984353 212 5.852 <.0001
H - N 0.2881375 3.744980 57 0.077 1.0000
A - F -4.0099069 2.242793 212 -1.788 0.4513
A - N -27.0382913 4.145370 57 -6.523 <.0001
F - N -23.0283844 4.359019 57 -5.283 <.0001
The answer is yes, emmeans does the calculation based on the model