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
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
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.
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 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.
I'm doing binary logistic regression in R, and some of the independent variables represent ordinal data. I just want to make sure I'm doing it correctly. In the example below, I created sample data and ran glm() based on the assumption that the independent variable "I" represents continuous data. Then I ran it again using ordered(I) instead. The results came out a little bit differently, so it seems like a successful test. My question is whether it's doing what I think it's doing...e.g., it's seeing the integer data, coercing it to ordinal data based on the values of the integers, and running the glm() with a different formula to account for the idea that the distance between "1," "2," "3," etc. may not be the same, hence making it "correct" if this represents ordinal data. Is that correct?
> str(gorilla)
'data.frame': 14 obs. of 2 variables:
$ I: int 1 1 1 2 2 2 3 3 4 4 ...
$ D: int 0 0 1 0 0 1 1 1 0 1 ...
> glm.out = glm(D ~ I, family=binomial(logit), data=gorilla)
> summary(glm.out)
...tried it again with ordered:
glm.out = glm(D ~ ordered(I), family=binomial(logit), data=gorilla)
> summary(glm.out)
PS: In case it would help, here's the full output from these tests (one thing I'm noticing is the very large standard error numbers):
Call:
glm(formula = D ~ I, family = binomial(logit), data = gorilla)
Deviance Residuals:
Min 1Q Median 3Q Max
-1.7067 -1.0651 0.7285 1.0137 1.4458
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -1.0624 1.2598 -0.843 0.399
I 0.4507 0.3846 1.172 0.241
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 19.121 on 13 degrees of freedom
Residual deviance: 17.621 on 12 degrees of freedom
AIC: 21.621
Number of Fisher Scoring iterations: 4
> glm.out = glm(D ~ ordered(I), family=binomial(logit), data=gorilla)
> summary(glm.out)
Call:
glm(formula = D ~ ordered(I), family = binomial(logit), data = gorilla)
Deviance Residuals:
Min 1Q Median 3Q Max
-1.66511 -0.90052 0.00013 0.75853 1.48230
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 3.6557 922.4405 0.004 0.997
ordered(I).L 1.3524 1.2179 1.110 0.267
ordered(I).Q -9.5220 2465.3259 -0.004 0.997
ordered(I).C 0.1282 1.2974 0.099 0.921
ordered(I)^4 13.6943 3307.5816 0.004 0.997
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 19.121 on 13 degrees of freedom
Residual deviance: 14.909 on 9 degrees of freedom
AIC: 24.909
Number of Fisher Scoring iterations: 17
Data used:
I,D
1,0
1,0
1,1
2,0
2,0
2,1
3,1
3,1
4,0
4,1
5,0
5,1
5,1
5,1