I am trying to extract fixed effects from two nlme models.
my reprex is
SAC<-data.frame(
row.names = c("1", "3", "4", "5", "6", "7"),
SZCug_dL = c(46.875, 50, 65.625, 62.5, 40.625, 62.5),
UrineZN_Cr = c(545.719546038266,
174.079978025994,915.970562853193,715.51555504984,
125.071835981547,488.546883216235),
z = c(3.84748448429057,
3.91202300542815,4.18395672091179,4.13516655674236,
3.7043836406499,4.13516655674236),
x = c(6.04681340416001,
4.36615081777328,6.61266697401435,6.85034696955171,
5.15700813886738,5.81233722866466),
HH = as.factor(c("1837", "30", "164", "164", "165", "200")),
CL = as.factor(c("303", "813", "459", "459", "459", "361"))
)
#> SZCug_dL UrineZN_Cr z x HH CL
#> 1 46.875 545.7195 3.847484 6.046813 1837 303
#> 3 50.000 174.0800 3.912023 4.366151 30 813
#> 4 65.625 915.9706 4.183957 6.612667 164 459
#> 5 62.500 715.5156 4.135167 6.850347 164 459
#> 6 40.625 125.0718 3.704384 5.157008 165 459
#> 7 62.500 488.5469 4.135167 5.812337 200 361
When I run the code and summary, I get an output without the fixed effects (most times) sometimes it appears and it's not as if I do anything different. What I usually get is
model1<-lme(z~1,data=df,random=~1|CL/HH)
summary(model1)
Linear mixed-effects model fit by REML
Data: df
Random effects:
Formula: ~1 | CL
(Intercept)
StdDev: 3.831037e-06
Formula: ~1 | HH %in% CL
(Intercept) Residual
StdDev: 0.1914616 0.03440302
Fixed effects: z ~ 1
Standardized Within-Group Residuals:
Min Q1 Med Q3 Max
-0.61342297 -0.19294160 -0.06602971 0.11545996 0.80477144
Number of Observations: 6
Number of Groups:
CL HH %in% CL
4 5
or
model2<-lme(z~x,data=df,random=~1|CL/HH)
> summary(model2)
Linear mixed-effects model fit by REML
Data: df
Random effects:
Formula: ~1 | CL
(Intercept)
StdDev: 3.09174e-06
Formula: ~1 | HH %in% CL
(Intercept) Residual
StdDev: 0.1264505 6.609614e-07
Fixed effects: z ~ x
Correlation:
(Intr)
x 0
Standardized Within-Group Residuals:
Min Q1 Med Q3 Max
-8.118371e-06 -4.120831e-06 9.382856e-07 4.479113e-06 6.386927e-06
Number of Observations: 6
Number of Groups:
CL HH %in% CL
4 5
for model 2.
However sometimes, weirdly enough, the fixed effects show up and when they do, it works for both models and I get something like this (note, this output below is not from the reprex, but from a previous run)
> summary(model2)
Linear mixed-effects model fit by REML
Data: SAC
AIC BIC logLik
-75.47897 -53.17945 42.73949
Random effects:
Formula: ~1 | CL
(Intercept)
StdDev: 0.1108957
Formula: ~1 | HH %in% CL
(Intercept) Residual
StdDev: 0.08661432 0.1934164
Fixed effects: z ~ x
Value Std.Error DF t-value p-value
(Intercept) 3.743666 0.05369683 315 69.71857 0
x 0.062271 0.00921621 315 6.75669 0
Correlation:
(Intr)
x -0.96
Standardized Within-Group Residuals:
Min Q1 Med Q3 Max
-3.63918369 -0.55392006 0.03131735 0.54203818 3.48252151
Number of Observations: 641
Number of Groups:
CL HH %in% CL
97 325
This problem occurs for both the null and the saturated model.
I need to extract these for about 30 models and have only gotten three so far after days of trying.
Related
For example, this is the result of certain multilevel analysis
MLM1<-lmer(y ~ 1 + con + ev1 + ev2 + (1 | pid),data=dat_ind)
Linear mixed model fit by REML. t-tests use Satterthwaite's method ['lmerModLmerTest']
Formula: y ~ 1 + con + ev1 + ev2 + (1 | pid)
Data: dat_ind
REML criterion at convergence: 837
Scaled residuals:
Min 1Q Median 3Q Max
-2.57771 -0.52765 0.00076 0.54715 2.27597
Random effects:
Groups Name Variance Std.Dev.
pid (Intercept) 1.4119 1.1882
Residual 0.9405 0.9698
Number of obs: 240, groups: pid, 120
Fixed effects:
Estimate Std. Error df t value Pr(>|t|)
(Intercept) 0.1727 0.1385 116.7062 1.247 0.21494
con 0.3462 0.1044 227.3108 3.317 0.00106 **
ev1 -0.3439 0.2083 116.8432 -1.651 0.10143
ev2 0.2525 0.1688 117.0168 1.495 0.13753
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Correlation of Fixed Effects:
(Intr) con ev1
con 0.031
ev1 0.171 -0.049
ev2 -0.423 0.065 -0.407
for example, I can extract fixed effect such as following.
summary(MLM1)[['coefficients']]['ev1','Pr(>|t|)']
How can I extract random effect coefficients?
for example, I want to extract 1.4119, 1.1882, 0.9405, 0.9698.
Random effects:
Groups Name Variance Std.Dev.
pid (Intercept) 1.4119 1.1882
Residual 0.9405 0.9698
The random effects results are not coefficients, but to get the variance and standard deviation as reported in the summary output, you can use the VarCorr function.
For example,
library(lme4)
#> Loading required package: Matrix
fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
summary(fm1)
#> Linear mixed model fit by REML ['lmerMod']
#> Formula: Reaction ~ Days + (Days | Subject)
#> Data: sleepstudy
#>
#> REML criterion at convergence: 1743.6
#>
#> Scaled residuals:
#> Min 1Q Median 3Q Max
#> -3.9536 -0.4634 0.0231 0.4634 5.1793
#>
#> Random effects:
#> Groups Name Variance Std.Dev. Corr
#> Subject (Intercept) 612.10 24.741
#> Days 35.07 5.922 0.07
#> Residual 654.94 25.592
#> Number of obs: 180, groups: Subject, 18
#>
#> Fixed effects:
#> Estimate Std. Error t value
#> (Intercept) 251.405 6.825 36.838
#> Days 10.467 1.546 6.771
#>
#> Correlation of Fixed Effects:
#> (Intr)
#> Days -0.138
If you want the results as a table you could do:
cbind(Var = diag(VarCorr(fm1)$Subject),
stddev = attr(VarCorr(fm1)$Subject, "stddev"))
#> Var stddev
#> (Intercept) 612.10016 24.740658
#> Days 35.07171 5.922138
Obviously, you'll need pid instead of Subject in the code above - we don't have your data or model for a demo here.
Created on 2022-04-27 by the reprex package (v2.0.1)
VarCorr(MLM1)$pid is the basic object.
broom.mixed::tidy(MLM1, effects = "ran_pars") may give you a more convenient format.
library(lme4)
fm1 <- lmer(Reaction ~ Days + (1|Subject), sleepstudy)
## RE variance
v1 <- VarCorr(fm1)$Subject
s1 <- attr(VarCorr(fm1)$Subject, "stddev")
## or
s1 <- sqrt(v1)
attr(VarCorr(fm1), "sc") ## residual std dev
## or
sigma(fm1)
## square these values if you want the residual variance
Or:
broom.mixed::tidy(fm1, effects = "ran_pars") ## std devs
broom.mixed::tidy(fm1, effects = "ran_pars", scales = "vcov") ## variances
for my research participants (n=N) ranked 4 pictures, producing 4*N cases.
Now I want to control for the participants and pictures. I included the participantID (here=PerNO) and pictureID, but it only works for PicID. What i do wrong?
Many thanks for any idea.
Here the output with participantID, after that you'll find the working output with PictureID
> summary(var.with.random)
Cumulative Link Mixed Model fitted with the Laplace approximation
formula: Int ~ PicSex + Posture + (1 | PerNO)
data: x
link threshold nobs logLik AIC niter max.grad cond.H
logit flexible 668 -917.62 1847.24 423(331) 3.80e-06 NaN
Random effects:
Groups Name Variance Std.Dev.
PerNO (Intercept) 5.38e-07 0.0007335
Number of groups: PerNO 167
Coefficients:
Estimate Std. Error z value Pr(>|z|)
PicSex1 0.4107 NA NA NA
Posture -0.4035 NA NA NA
Threshold coefficients:
Estimate Std. Error z value
1|2 -1.12189 NA NA
2|3 -0.01118 NA NA
3|4 1.11189 NA NA
```
**Output with PictureID:**
```
> var.with.random <- clmm(Int~PicSex + Posture +(1|PicID), data = x)
> summary(var.with.random)
Cumulative Link Mixed Model fitted with the Laplace approximation
formula: Int ~ PicSex + Posture + (1 | PicID)
data: x
link threshold nobs logLik AIC niter max.grad cond.H
logit flexible 668 -896.18 1804.35 317(954) 7.20e-04 1.2e+02
Random effects:
Groups Name Variance Std.Dev.
PicID (Intercept) 0.3262 0.5711
Number of groups: PicID 16
Coefficients:
Estimate Std. Error z value Pr(>|z|)
PicSex1 0.4428 0.3195 1.386 0.166
Posture -0.4528 0.3198 -1.416 0.157
Threshold coefficients:
Estimate Std. Error z value
1|2 -1.21490 0.28418 -4.275
2|3 -0.01622 0.27953 -0.058
3|4 1.18597 0.28276 4.194
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 trying to write a .csv file that appends the important information from the summary of a glmer analysis (from the package lme4).
I have been able to isolate the coefficients, AIC, and random effects , but I have not been able to isolate the scaled residuals (Min, 1Q, Median, 3Q, Max).
I have tried using $residuals, but I get a very long output, not the information shown in the summary.
> library(lme4)
> setwd("C:/Users/Arthur Scully/Dropbox/! ! ! ! PHD/Chapter 2 Lynx Bobcat BC/ResourceSelection")
> #simple vectors
>
> x <- c("a","b","b","b","b","d","b","c","c","a")
>
> y <- c(1,1,0,1,0,1,1,1,1,0)
>
>
> # Simple data frame
>
> aes.samp <- data.frame(x,y)
> aes.samp
x y
1 a 1
2 b 1
3 b 0
4 b 1
5 b 0
6 d 1
7 b 1
8 c 1
9 c 1
10 a 0
>
> # Simple glmer
>
> aes.glmer <- glmer(y~(1|x),aes.samp,family ="binomial")
boundary (singular) fit: see ?isSingular
>
> summary(aes.glmer)
Generalized linear mixed model fit by maximum likelihood (Laplace Approximation) ['glmerMod']
Family: binomial ( logit )
Formula: y ~ (1 | x)
Data: aes.samp
AIC BIC logLik deviance df.resid
16.2 16.8 -6.1 12.2 8
I can isolate information above by using the call summary(aes.glmer)$AIC
Scaled residuals:
Min 1Q Median 3Q Max
-1.5275 -0.9820 0.6546 0.6546 0.6546
I do not know the call to isolate the above information
Random effects:
Groups Name Variance Std.Dev.
x (Intercept) 0 0
Number of obs: 10, groups: x, 4
I can isolate this information using the ranef function
Fixed effects:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 0.8473 0.6901 1.228 0.22
And I can isolate the information above using summary(aes.glmer)$coefficient
convergence code: 0
boundary (singular) fit: see ?isSingular
>
> #Pull important
> ##write call to select important output
> aes.glmer.coef <- summary(aes.glmer)$coefficient
> aes.glmer.AIC <- summary(aes.glmer)$AIC
> aes.glmer.ran <-ranef(aes.glmer)
>
> ##
> data.frame(c(aes.glmer.coef, aes.glmer.AIC, aes.glmer.ran))
X0.847297859077025 X0.690065555425105 X1.22785125618255 X0.219502810378876 AIC BIC logLik deviance df.resid X.Intercept.
a 0.8472979 0.6900656 1.227851 0.2195028 16.21729 16.82246 -6.108643 12.21729 8 0
b 0.8472979 0.6900656 1.227851 0.2195028 16.21729 16.82246 -6.108643 12.21729 8 0
c 0.8472979 0.6900656 1.227851 0.2195028 16.21729 16.82246 -6.108643 12.21729 8 0
d 0.8472979 0.6900656 1.227851 0.2195028 16.21729 16.82246 -6.108643 12.21729 8 0
If anyone knows what call I can use to isolate the "scaled residuals" I would be very greatful.
I haven't got your data, so we'll use example data from the lme4 vignette.
library(lme4)
library(lattice)
library(broom)
gm1 <- glmer(cbind(incidence, size - incidence) ~ period + (1 | herd),
data = cbpp, family = binomial)
This is for the residuals. tidy from the broom package puts it in to a tibble, which you can then export to a csv.
x <- tidy(quantile(residuals(gm1, "pearson", scaled = TRUE)))
x
# A tibble: 5 x 2
names x
<chr> <dbl>
1 0% -2.38
2 25% -0.789
3 50% -0.203
4 75% 0.514
5 100% 2.88
Also here are some of the other bits that you might find useful, using glance from broom.
y <- glance(gm1)
y
# A tibble: 1 x 6
sigma logLik AIC BIC deviance df.residual
<dbl> <dbl> <dbl> <dbl> <dbl> <int>
1 1 -92.0 194. 204. 73.5 51
And
z <- tidy(gm1)
z
# A tibble: 5 x 6
term estimate std.error statistic p.value group
<chr> <dbl> <dbl> <dbl> <dbl> <chr>
1 (Intercept) -1.40 0.231 -6.05 1.47e-9 fixed
2 period2 -0.992 0.303 -3.27 1.07e-3 fixed
3 period3 -1.13 0.323 -3.49 4.74e-4 fixed
4 period4 -1.58 0.422 -3.74 1.82e-4 fixed
5 sd_(Intercept).herd 0.642 NA NA NA herd
I am a beginner with R. I am using glm to conduct logistic regression and then using the 'margins' package to calculate marginal effects but I don't seem to be able to exclude the missing values in my categorical independent variable.
I have tried to ask R to exclude NAs from the regression. The categorical variable is weight status at age 9 (wgt9), and it has three levels (1, 2, 3) and some NAs.
What am I doing wrong? Why do I get a wgt9NA result in my outputs and how can I correct it?
Thanks in advance for any help/advice.
Conduct logistic regression
summary(logit.phbehav <- glm(obese13 ~ gender + as.factor(wgt9) + aded08b,
data = gui, weights = bdwg01, family = binomial(link = "logit")))
Regression output
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) -3.99 0.293 -13.6 2.86e- 42
2 gender 0.387 0.121 3.19 1.42e- 3
3 as.factor(wgt9)2 2.49 0.177 14.1 3.28e- 45
4 as.factor(wgt9)3 4.65 0.182 25.6 4.81e-144
5 as.factor(wgt9)NA 2.60 0.234 11.1 9.94e- 29
6 aded08b -0.0755 0.0224 -3.37 7.47e- 4
Calculate the marginal effects
effects_logit_phtotal = margins(logit.phtot)
print(effects_logit_phtotal)
summary(effects_logit_phtotal)
Marginal effects output
> summary(effects_logit_phtotal)
factor AME SE z p lower upper
aded08a -0.0012 0.0002 -4.8785 0.0000 -0.0017 -0.0007
gender 0.0115 0.0048 2.3899 0.0169 0.0021 0.0210
wgt92 0.0941 0.0086 10.9618 0.0000 0.0773 0.1109
wgt93 0.4708 0.0255 18.4569 0.0000 0.4208 0.5207
wgt9NA 0.1027 0.0179 5.7531 0.0000 0.0677 0.1377
First of all welcome to stack overflow. Please check the answer here to see how to make a great R question. Not providing a sample of your data, some times makes it impossible to answer the question. However taking a guess, I think that you have not set your NA values correctly but as strings. This behavior can be seen in the dummy data below.
First let's create the dummy data:
v1 <- c(2,3,3,3,2,2,2,2,NA,NA,NA)
v2 <- c(2,3,3,3,2,2,2,2,"NA","NA","NA")
v3 <- c(11,5,6,7,10,8,7,6,2,5,3)
obese <- c(0,1,1,0,0,1,1,1,0,0,0)
df <- data.frame(obese,v1,v2)
Using the variable named v1, does not include NA as a category:
glm(formula = obese ~ as.factor(v1) + v3, family = binomial(link = "logit"),
data = df)
Deviance Residuals:
1 2 3 4 5 6 7 8
-2.110e-08 2.110e-08 1.168e-05 -1.105e-05 -2.110e-08 3.094e-06 2.110e-08 2.110e-08
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 401.48 898581.15 0 1
as.factor(v1)3 -96.51 326132.30 0 1
v3 -46.93 106842.02 0 1
While making the string "NA" to factor gives an output similar to the one in question:
glm(formula = obese ~ as.factor(v2) + v3, family = binomial(link = "logit"),
data = df)
Deviance Residuals:
Min 1Q Median 3Q Max
-1.402e-05 -2.110e-08 -2.110e-08 2.110e-08 1.472e-05
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 394.21 744490.08 0.001 1
as.factor(v2)3 -95.33 340427.26 0.000 1
as.factor(v2)NA -327.07 613934.84 -0.001 1
v3 -45.99 84477.60 -0.001 1
Try the following to replace NAs that are strings:
gui$wgt9[ gui$wgt9 == "NA" ] <- NA
Don't forget to accept any answer that solved your problem.