extract AIC from a fixed effect spatial panel model estimation result - r

I am using the package 'splm' of Millo and Piras(2012) to estimate a SPatial Durbin model with country (individual) fixed effect.
ks = log(spa.sak$index_ret+2) ~ log(spa.sak$lagindex+2) + log(spa.sak$cred_def_rate+2) + log(spa.sak$ch_in_ex_rate+2) +
log(spa.sak$un_inf+2) + log(spa.sak$gdp_growth+2) + log(spa.sak$cha_in_int_rate+2) + country
ERVM<- spml(formula = ks, data = spa.sak, index = c('id','months'), listw = ERV, model = "within", lag = TRUE, effect = "individual", spatial.error = "none")
> summary(ERVM)
Spatial panel fixed effects lag model
Call:
spml(formula = ks, data = spa.sak, index = c("id", "months"),
listw = ERV, model = "within", effect = "individual", lag = TRUE,
spatial.error = "none")
Residuals:
Min. 1st Qu. Median 3rd Qu. Max.
-0.783343 -0.028334 -0.003056 0.020040 1.199906
Spatial autoregressive coefficient:
Estimate Std. Error t-value Pr(>|t|)
lambda -0.0040442 0.0061785 -0.6546 0.5127
Coefficients:
Estimate Std. Error t-value Pr(>|t|)
log(spa.sak$lagindex + 2) -2.5577e-01 1.6593e-02 -15.4142 < 2.2e-16 ***
log(spa.sak$cred_def_rate + 2) 1.3427e-02 2.3244e-02 0.5777 0.5635
log(spa.sak$ch_in_ex_rate + 2) -5.1946e-04 2.2338e-03 -0.2325 0.8161
log(spa.sak$un_inf + 2) 1.1360e-01 2.2112e-01 0.5138 0.6074
log(spa.sak$gdp_growth + 2) 3.1093e+00 2.1339e+00 1.4571 0.1451
log(spa.sak$cha_in_int_rate + 2) -1.4148e+01 3.1507e+00 -4.4905 7.105e-06 ***
countryEGYPT 1.0659e-02 1.7940e-02 0.5942 0.5524
countryGHANA 2.3214e-02 2.0567e-02 1.1287 0.2590
countryKENYA 1.6923e-02 1.9599e-02 0.8634 0.3879
countryMALAWI 1.0190e-02 3.2028e-02 0.3182 0.7504
countryMAURITIUS 2.5843e-03 1.3530e-02 0.1910 0.8485
countryMOROCCO 4.2243e-03 1.4848e-02 0.2845 0.7760
countryNAMIBIA -1.0416e-02 1.4408e-02 -0.7229 0.4697
countryNIGERIA 1.2794e-02 1.8327e-02 0.6981 0.4851
countrySOUTH AFRICA -9.4671e-04 1.3665e-02 -0.0693 0.9448
countryTANZANIA 1.9698e-02 1.9822e-02 0.9937 0.3204
countryTUNISIA 1.4579e-02 1.4895e-02 0.9788 0.3277
countryUGANDA 3.1850e-02 2.1042e-02 1.5137 0.1301
countryZAMBIA 3.3671e-02 2.1202e-02 1.5881 0.1123
countryZIMBABWE 1.4299e-02 3.1798e-02 0.4497 0.6529
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> str(ERVM)
List of 15
$ coefficients : Named num [1:21] -0.004044 -0.255767 0.013427 -0.000519 0.113601 ...
..- attr(*, "names")= chr [1:21] "lambda" "log(spa.sak$lagindex + 2)" "log(spa.sak$cred_def_rate + 2)" "log(spa.sak$ch_in_ex_rate + 2)" ...
$ errcomp : NULL
$ vcov : num [1:21, 1:21] 3.82e-05 0.00 0.00 0.00 0.00 ...
$ spat.coef : Named num -0.00404
..- attr(*, "names")= chr "lambda"
$ vcov.errcomp : NULL
$ residuals : Named num [1:3390] -0.1195 0.0395 0.0151 -0.0186 0.0285 ...
..- attr(*, "names")= chr [1:3390] "5-01/01/2000" "5-01/01/2001" "5-01/01/2002" "5-01/01/2003" ...
$ fitted.values: num [1:3390, 1] 0.681 0.712 0.704 0.725 0.719 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:3390] "5-01/01/2000" "5-01/01/2001" "5-01/01/2002" "5-01/01/2003" ...
.. ..$ : NULL
..- attr(*, "names")= chr [1:3390] "5-01/01/2000" "5-01/01/2001" "5-01/01/2002" "5-01/01/2003" ...
$ sigma2 : num 0.0192
$ type : chr "fixed effects lag"
$ model :'data.frame': 3390 obs. of 21 variables:
..$ y : num [1:3390] 0.537 0.731 0.697 0.685 0.728 ...
..$ log.spa.sak.lagindex...2. : num [1:3390] 0.851 0.71 0.752 0.674 0.731 ...
..$ log.spa.sak.cred_def_rate...2. : num [1:3390] 2.83 2.83 2.83 2.83 2.83 ...
..$ log.spa.sak.ch_in_ex_rate...2. : num [1:3390] 0.696 0.69 0.711 0.768 0.695 ...
..$ log.spa.sak.un_inf...2. : num [1:3390] 0.697 0.694 0.693 0.692 0.694 ...
..$ log.spa.sak.gdp_growth...2. : num [1:3390] 0.693 0.693 0.693 0.693 0.693 ...
..$ log.spa.sak.cha_in_int_rate...2.: num [1:3390] 0.693 0.693 0.693 0.693 0.693 ...
..$ countryEGYPT : num [1:3390] 0 0 0 0 0 0 0 0 0 0 ...
..$ countryGHANA : num [1:3390] 0 0 0 0 0 0 0 0 0 0 ...
..$ countryKENYA : num [1:3390] 0 0 0 0 0 0 0 0 0 0 ...
..$ countryMALAWI : num [1:3390] 0 0 0 0 0 0 0 0 0 0 ...
..$ countryMAURITIUS : num [1:3390] 0 0 0 0 0 0 0 0 0 0 ...
..$ countryMOROCCO : num [1:3390] 0 0 0 0 0 0 0 0 0 0 ...
..$ countryNAMIBIA : num [1:3390] 0 0 0 0 0 0 0 0 0 0 ...
..$ countryNIGERIA : num [1:3390] 0 0 0 0 0 0 0 0 0 0 ...
..$ countrySOUTH.AFRICA : num [1:3390] 0 0 0 0 0 0 0 0 0 0 ...
..$ countryTANZANIA : num [1:3390] 0 0 0 0 0 0 0 0 0 0 ...
..$ countryTUNISIA : num [1:3390] 0 0 0 0 0 0 0 0 0 0 ...
..$ countryUGANDA : num [1:3390] 0 0 0 0 0 0 0 0 0 0 ...
..$ countryZAMBIA : num [1:3390] 0 0 0 0 0 0 0 0 0 0 ...
..$ countryZIMBABWE : num [1:3390] 0 0 0 0 0 0 0 0 0 0 ...
$ call : language spml(formula = ks, data = spa.sak, index = c("id", "months"), listw = ERV, model = "within", effect = "individual| __truncated__
$ logLik : num 1895
$ method : chr "eigen"
$ effects : chr "spfe"
$ res.eff :List of 2
..$ :List of 7
.. ..$ res.sfe : num [1:15, 1] 0.00264 -0.00152 0.0017 0.0023 0.0106 ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr [1:15] "1" "2" "3" "4" ...
.. .. .. ..$ : NULL
.. ..$ res.se.sfe: Named num [1:15] 2.55 2.55 2.55 2.55 2.55 ...
.. .. ..- attr(*, "names")= chr [1:15] "1" "2" "3" "4" ...
.. ..$ intercept : num [1, 1] 8.43
.. ..$ res.se.con: num [1, 1] 2.55
.. ..$ xhat : num [1:3390, 1] 0.681 0.712 0.704 0.725 0.719 ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr [1:3390] "5-01/01/2000" "5-01/01/2001" "5-01/01/2002" "5-01/01/2003" ...
.. .. .. ..$ : NULL
.. ..$ N.vars : int 35
.. ..$ res.e : num [1:3390, 1] -0.1195 0.0395 0.0151 -0.0186 0.0285 ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr [1:3390] "5-01/01/2000" "5-01/01/2001" "5-01/01/2002" "5-01/01/2003" ...
.. .. .. ..$ : NULL
..$ res.corr: NULL
- attr(*, "class")= chr "splm"
I am successful to obtain estimation results except the following two.
1) As it is shown above, I am only able to get values for \lambda and \beta coefficients. I can not get the \theta estimation result or value that relates with the spatially lagged explanatory variables which shows the spatio-temporal relationships. How can extract it?
2) I am not able to extract AIC and BIC for model comparison.
In the first case, my trials weren't successful, and
For the second case, I have tried
AICsplm(ERVMX, criterion="AIC")
But I got an error.
How can I fix these issues?
UPDATE: I have solved the issue and please refer below.

Construct the spatial lag for both response and predictor variable in advance and add in to the formula and run.
As far as AIC is concerned, there is a need to download AICsplm.R from github download AICsplm.R to run the code.
source(AICsplm.R)
AICsplm(ERVM)

Based on the answer in this question, this function also works for splm objects:
AIC_adj <- function(mod){
# Number of observations
n.N <- nrow(mod$model)
# Residuals vector
u.hat <- residuals(mod)
# Variance estimation
s.sq <- log( (sum(u.hat^2)/(n.N)))
# Number of parameters (incl. constant) + one additional for variance estimation
p <- length(coef(mod)) + 1
# Note: minus sign cancels in log likelihood
aic <- 2*p + n.N * ( log(2*pi) + s.sq + 1 )
return(aic)
}

Related

Composite scores for consensus clustering in R

I am using ConsensusClusterPlus package in R for clustering my omic data. I want to use my clusters for regression.Is there a way to create composite scores if say i reduce 1000 genes to 7 clusters and use those 7 clusters for regression.
I tried to look at structure of cluster in R.
results = ConsensusClusterPlus(d1,maxK=maxK,reps=1000,pItem=0.8,pFeature=1, title=title,clusterAlg="hc",distance="pearson",seed=1262118388.71279,plot="png")
icl = calcICL(results,title=title,plot="png")
str(results[[7]])
List of 5
$ consensusMatrix: num [1:40, 1:40] 1 0.689 0.976 1 1 ...
$ consensusTree :List of 7
..$ merge : int [1:39, 1:2] -1 -5 -7 -8 -9 -10 -11 -12 -13 -14 ...
..$ height : num [1:39] 0 0 0 0 0 0 0 0 0 0 ...
..$ order : int [1:40] 40 34 35 28 6 32 22 18 21 19 ...
..$ labels : NULL
..$ method : chr "average"
..$ call : language hclust(d = as.dist(1 - fm), method = finalLinkage)
..$ dist.method: NULL
..- attr(*, "class")= chr "hclust"
$ consensusClass : Named int [1:40] 1 1 1 1 1 2 1 1 1 1 ...
..- attr(*, "names")= chr [1:40] "CAR 12:0" "CAR 12:1" "CAR 13:0" "CAR 14:0" ...
$ ml : num [1:40, 1:40] 1 0.689 0.976 1 1 ...
$ clrs :List of 3
..$ : chr [1:40] "#A6CEE3" "#A6CEE3" "#A6CEE3" "#A6CEE3" ...
..$ : num 8
..$ : chr [1:7] "#A6CEE3" "#FB9A99" "#FF7F00" "#FDBF6F" ...
How to find composite scores ?

MCMCglmm interaction plots in R with ggeffects-package or sjPlot-package

I have done many bayesian models using the MCMCglmm package in R, like this one:
model=MCMCglmm(scale(lifespan)~scale(weight)*scale(littersize),
random=~idv(DNA1)+idv(DNA2),
data=df,
family="gaussian",
prior=prior1,
thin=50,
burnin=5000,
nitt=50000,
verbose=F)
summary(model)
post.mean l-95% CI u-95% CI eff.samp pMCMC
(Intercept) 11.23327 8.368 13.73756 6228 <2e-04 ***
weight -1.63770 -2.059 -1.23457 6600 <2e-04 ***
littersize 0.40960 0.024 0.80305 6600 0.0415 *
weight:littersize -0.33411 -0.635 -0.04406 5912 0.0248 *
I would like to plot the resulting interaction (weight:littersize) with ggeffects or sjPlots packages, like this:
plot_model(model,
type = "int",
terms = c("scale(lifespan)", "scale(weight)", "scale(littersize)"),
mdrt.values = "meansd",
ppd = TRUE)
But I obtain the next output:
`scale(weight)` was not found in model terms. Maybe misspelled?
`scale(littersize)` was not found in model terms. Maybe misspelled?
Error in terms.default(model) : no terms component nor attribute
Además: Warning messages:
1: Some model terms could not be found in model data. You probably need to load the data into the environment.
2: Some model terms could not be found in model data. You probably need to load the data into the environment.
Data is already loaded. I tried to write terms differently without the "scale(x)" term, and changed the model too to deal with equal terms, but I am still getting this error message. I am also open to plot this interaction with different packages.
My model str(model) is:
>str(model)
List of 20
$ Sol : 'mcmc' num [1:6600, 1:4] -0.814 1.215 -2.119 -0.125 -1.648 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : NULL
.. ..$ : chr [1:4] "(Intercept)" "scale(weight)" "scale(littersize)" "scale(weight):scale(littersize)"
..- attr(*, "mcpar")= num [1:3] 7e+04 4e+05 5e+01
$ Lambda : NULL
$ VCV : 'mcmc' num [1:6600, 1:3] 1.094 0.693 1.58 0.645 1.161 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : NULL
.. ..$ : chr [1:3] "phylo." "haplo." "units"
..- attr(*, "mcpar")= num [1:3] 7e+04 4e+05 5e+01
$ CP : NULL
$ Liab : NULL
$ Fixed :List of 3
..$ formula:Class 'formula' language scale(lifespan) ~ scale(weight) * scale(littersize)
.. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
..$ nfl : int 4
..$ nll : num 0
$ Random :List of 5
..$ formula:Class 'formula' language ~idv(phylo) + idv(haplo)
.. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
..$ nfl : num [1:2] 1 1
..$ nrl : int [1:2] 92 92
..$ nat : num [1:2] 0 0
..$ nrt : int [1:2] 1 1
$ Residual :List of 6
..$ formula :Class 'formula' language ~units
.. .. ..- attr(*, ".Environment")=<environment: 0x0000025ba05f8938>
..$ nfl : num 1
..$ nrl : int 92
..$ nrt : int 1
..$ family : chr "gaussian"
..$ original.family: chr "gaussian"
$ Deviance : 'mcmc' num [1:6600] -262.6 -137.3 -203.6 -83.6 -29.1 ...
..- attr(*, "mcpar")= num [1:3] 7e+04 4e+05 5e+01
$ DIC : num -158
$ X :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
.. ..# i : int [1:368] 0 1 2 3 4 5 6 7 8 9 ...
.. ..# p : int [1:5] 0 92 184 276 368
.. ..# Dim : int [1:2] 92 4
.. ..# Dimnames:List of 2
.. .. ..$ : chr [1:92] "1.1" "2.1" "3.1" "4.1" ...
.. .. ..$ : chr [1:4] "(Intercept)" "scale(weight)" "scale(littersize)" "scale(weight):scale(littersize)"
.. ..# x : num [1:368] 1 1 1 1 1 1 1 1 1 1 ...
.. ..# factors : list()
$ Z :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
.. ..# i : int [1:16928] 0 1 2 3 4 5 6 7 8 9 ...
.. ..# p : int [1:185] 0 92 184 276 368 460 552 644 736 828 ...
.. ..# Dim : int [1:2] 92 184
.. ..# Dimnames:List of 2
.. .. ..$ : NULL
.. .. ..$ : chr [1:184] "phylo1.NA.1" "phylo2.NA.1" "phylo3.NA.1" "phylo4.NA.1" ...
.. ..# x : num [1:16928] 0.4726 0.0869 0.1053 0.087 0.1349 ...
.. ..# factors : list()
$ ZR :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
.. ..# i : int [1:92] 0 1 2 3 4 5 6 7 8 9 ...
.. ..# p : int [1:93] 0 1 2 3 4 5 6 7 8 9 ...
.. ..# Dim : int [1:2] 92 92
.. ..# Dimnames:List of 2
.. .. ..$ : NULL
.. .. ..$ : chr [1:92] "units.1" "units.2" "units.3" "units.4" ...
.. ..# x : num [1:92] 1 1 1 1 1 1 1 1 1 1 ...
.. ..# factors : list()
$ XL : NULL
$ ginverse : NULL
$ error.term : int [1:92] 1 1 1 1 1 1 1 1 1 1 ...
$ family : chr [1:92] "gaussian" "gaussian" "gaussian" "gaussian" ...
$ Tune : num [1, 1] 1
..- attr(*, "dimnames")=List of 2
.. ..$ : chr "1"
.. ..$ : chr "1"
$ meta : logi FALSE
$ y.additional: num [1:92, 1:2] 0 0 0 0 0 0 0 0 0 0 ...
- attr(*, "class")= chr "MCMCglmm"
Thank you.
Try to scale your predictors before fitting the model, i.e.
df$lifespan <- as.vecor(scale(df$lifespan))
Or better, use effectsize::standardize(), which does not create a matrix for a one-dimensial vector when scaling your variables:
df <- effectsize::standardize(df, select = c("lifespan", "weight", "littersize"))
Then you can call your model like this:
model <- MCMCglmm(lifespan ~ weight * littersize,
random=~idv(DNA1)+idv(DNA2),
data=df,
family="gaussian",
prior=prior1,
thin=50,
burnin=5000,
nitt=50000,
verbose=F)
Does this work?

Extract only the forecasted values from forecast() [duplicate]

This question already has an answer here:
in R, extract part of object from list
(1 answer)
Closed 4 years ago.
I have a dataframe that looks like this:
> head(forecasts)
$`1_1`
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Dec 2016 7.370299 7.335176 7.405422 7.316583 7.424015
$`1_10`
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Dec 2016 7.396656 7.359845 7.433467 7.340359 7.452953
$`1_2`
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Dec 2016 7.780033 7.752462 7.807605 7.737866 7.822201
$`1_3`
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Dec 2016 7.216894 7.178896 7.254892 7.158781 7.275007
$`1_4`
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Dec 2016 7.501195 7.465049 7.537341 7.445915 7.556475
$`1_5`
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Dec 2016 7.455131 7.424918 7.485345 7.408924 7.501339
I would like to extract only the Point Forecast
A call to str(forecasts) returns a lot of output, this is the output for just one of the 89 lists in the 'forecasts' variable:
$ 9_9 :List of 10
..$ method : chr "ARIMA(0,0,0)(0,1,0)[12] with drift"
..$ model :List of 19
.. ..$ coef : Named num 0.00965
.. .. ..- attr(*, "names")= chr "drift"
.. ..$ sigma2 : num 0.0047
.. ..$ var.coef : num [1, 1] 1.24e-06
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr "drift"
.. .. .. ..$ : chr "drift"
.. ..$ mask : logi TRUE
.. ..$ loglik : num 33.4
.. ..$ aic : num -62.7
.. ..$ arma : int [1:7] 0 0 0 0 12 0 1
.. ..$ residuals: Time-Series [1:38] from 2014 to 2017: 0.00546 0.00583 0.006 0.00564 0.00563 ...
.. ..$ call : language .f(y = .x[[i]], x = list(x = c(5.4677292870219, 5.85045765518954, 6.02852764863892, 5.67941181324485, 5.67526620| __truncated__ ...
.. ..$ series : chr ".x[[i]]"
.. ..$ code : int 0
.. ..$ n.cond : int 0
.. ..$ nobs : int 26
.. ..$ model :List of 10
.. .. ..$ phi : num(0)
.. .. ..$ theta: num(0)
.. .. ..$ Delta: num [1:12] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..$ Z : num [1:13] 1 0 0 0 0 0 0 0 0 0 ...
.. .. ..$ a : num [1:13] 0.0677 5.6916 5.7073 5.692 5.7108 ...
.. .. ..$ P : num [1:13, 1:13] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..$ T : num [1:13, 1:13] 0 1 0 0 0 0 0 0 0 0 ...
.. .. ..$ V : num [1:13, 1:13] 1 0 0 0 0 0 0 0 0 0 ...
.. .. ..$ h : num 0
.. .. ..$ Pn : num [1:13, 1:13] 1 0 0 0 0 0 0 0 0 0 ...
.. ..$ xreg : int [1:38, 1] 1 2 3 4 5 6 7 8 9 10 ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : NULL
.. .. .. ..$ : chr "drift"
.. ..$ bic : num -60.2
.. ..$ aicc : num -62.2
.. ..$ x : Time-Series [1:38] from 2014 to 2017: 5.47 5.85 6.03 5.68 5.68 ...
.. ..$ fitted : Time-Series [1:38] from 2014 to 2017: 5.46 5.84 6.02 5.67 5.67 ...
.. ..- attr(*, "class")= chr [1:2] "ARIMA" "Arima"
..$ level : num [1:2] 80 95
..$ mean : Time-Series [1:1] from 2017 to 2017: 6.32
..$ lower : Time-Series [1, 1:2] from 2017 to 2017: 6.23 6.18
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : NULL
.. .. ..$ : chr [1:2] "80%" "95%"
..$ upper : Time-Series [1, 1:2] from 2017 to 2017: 6.4 6.45
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : NULL
.. .. ..$ : chr [1:2] "80%" "95%"
..$ x : Time-Series [1:38] from 2014 to 2017: 5.47 5.85 6.03 5.68 5.68 ...
..$ series : chr ".x[[i]]"
..$ fitted : Time-Series [1:38] from 2014 to 2017: 5.46 5.84 6.02 5.67 5.67 ...
..$ residuals: Time-Series [1:38] from 2014 to 2017: 0.00546 0.00583 0.006 0.00564 0.00563 ...
..- attr(*, "class")= chr "forecast"
We can extract the values from the list using lapply
lapply(forecasts, function(x) as.numeric(x$mean, na.rm = TRUE)))
If the number of forecast are same in all the list elements, this can be converted to a matrix or data.frame
sapply(forecasts, `[[`, "mean")
Or using tidyverse
library(tidyverse)
forecasts %>%
map_df(~ .x$mean %>%
as.numeric)

How to extract "fixed effect design matrix" from glmer model object

The call mF#X should return fixed effect design matrix (necessary for marginal and conditional R^2 in glmm).
However it does not work.
Is even such matrix listed in model structure?
I appreciate any suggestions.
str(mF)
Formal class 'glmerMod' [package "lme4"] with 13 slots
..# resp :Reference class 'glmResp' [package "lme4"] with 11 fields
.. ..$ Ptr :<externalptr>
.. ..$ mu : num [1:480] 0.168 0.356 0.168 0.356 0.284 ...
.. ..$ offset : num [1:480] 0 0 0 0 0 0 0 0 0 0 ...
.. ..$ sqrtXwt: num [1:480] 0.373 0.479 0.373 0.479 0.451 ...
.. ..$ sqrtrwt: num [1:480] 2.68 2.09 2.68 2.09 2.22 ...
.. ..$ weights: num [1:480] 1 1 1 1 1 1 1 1 1 1 ...
.. ..$ wtres : num [1:480] -0.449 -0.744 -0.449 1.344 -0.629 ...
.. ..$ y : num [1:480] 0 0 0 1 0 0 0 1 0 0 ...
.. ..$ eta : num [1:480] -1.603 -0.591 -1.603 -0.591 -0.927 ...
.. ..$ family :List of 11
.. .. ..$ family : chr "binomial"
.. .. ..$ link : chr "logit"
.. .. ..$ linkfun :function (mu)
.. .. ..$ linkinv :function (eta)
.. .. ..$ variance :function (mu)
.. .. ..$ dev.resids:function (y, mu, wt)
.. .. ..$ aic :function (y, n, mu, wt, dev)
.. .. ..$ mu.eta :function (eta)
.. .. ..$ validmu :function (mu)
.. .. ..$ valideta :function (eta)
.. .. ..$ simulate :function (object, nsim)
.. .. ..- attr(*, "class")= chr "family"
.. ..$ n : num [1:480] 1 1 1 1 1 1 1 1 1 1 ...
.. ..and 41 methods, of which 29 are possibly relevant:
.. .. aic, allInfo, allInfo#lmResp, copy#envRefClass, devResid, fam,
.. .. initialize, initialize#lmResp, initializePtr, Laplace, link, muEta, ptr,
.. .. ptr#lmResp, resDev, setOffset, setResp, setTheta, setWeights, sqrtWrkWt,
.. .. theta, updateMu, updateMu#lmResp, updateWts, variance, wrkResids,
.. .. wrkResp, wrss, wtWrkResp
..# Gp : int [1:3] 0 60 72
..# call : language lme4::glmer(formula = Colour ~ Treatment + Habitat + (1 | Population) + (1 | Container), data = Data, family = "binomial", control = structure(list( ...
..# frame :'data.frame': 480 obs. of 5 variables:
.. ..$ Colour : int [1:480] 0 0 0 1 0 0 0 1 0 0 ...
.. ..$ Treatment : Factor w/ 2 levels "Cont","Exp": 1 2 1 2 1 2 1 2 1 2 ...
.. ..$ Habitat : Factor w/ 2 levels "A","B": 1 1 1 1 2 2 2 2 1 1 ...
.. ..$ Population: int [1:480] 1 1 1 1 1 1 1 1 1 1 ...
.. ..$ Container : int [1:480] 2 2 2 2 2 2 2 2 4 4 ...
.. ..- attr(*, "terms")=Classes 'terms', 'formula' length 3 Colour ~ Treatment + Habitat + (1 + Population) + (1 + Container)
.. .. .. ..- attr(*, "variables")= language list(Colour, Treatment, Habitat, Population, Container)
.. .. .. ..- attr(*, "factors")= int [1:5, 1:4] 0 1 0 0 0 0 0 1 0 0 ...
.. .. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. .. ..$ : chr [1:5] "Colour" "Treatment" "Habitat" "Population" ...
.. .. .. .. .. ..$ : chr [1:4] "Treatment" "Habitat" "Population" "Container"
.. .. .. ..- attr(*, "term.labels")= chr [1:4] "Treatment" "Habitat" "Population" "Container"
.. .. .. ..- attr(*, "order")= int [1:4] 1 1 1 1
.. .. .. ..- attr(*, "intercept")= int 1
.. .. .. ..- attr(*, "response")= int 1
.. .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
.. .. .. ..- attr(*, "predvars")= language list(Colour, Treatment, Habitat, Population, Container)
.. .. .. ..- attr(*, "dataClasses")= Named chr [1:5] "numeric" "factor" "factor" "numeric" ...
.. .. .. .. ..- attr(*, "names")= chr [1:5] "Colour" "Treatment" "Habitat" "Population" ...
.. .. .. ..- attr(*, "predvars.fixed")= language list(Colour, Treatment, Habitat)
.. ..- attr(*, "formula")=Class 'formula' length 3 Colour ~ Treatment + Habitat + (1 | Population) + (1 | Container)
.. .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
..# flist :List of 2
.. ..$ Container : Factor w/ 60 levels "2","4","6","8",..: 1 1 1 1 1 1 1 1 2 2 ...
.. ..$ Population: Factor w/ 12 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...
.. ..- attr(*, "assign")= int [1:2] 1 2
..# cnms :List of 2
.. ..$ Container : chr "(Intercept)"
.. ..$ Population: chr "(Intercept)"
..# lower : num [1:2] 0 0
..# theta : num [1:2] 0.0761 1.0537
..# beta : num [1:3] -1.251 1.012 0.676
..# u : num [1:72] -0.05 0.0254 -0.05 0.1008 -0.05 ...
..# devcomp:List of 2
.. ..$ cmp : Named num [1:11] 27.3 8.5 438.1 10.6 448.7 ...
.. .. ..- attr(*, "names")= chr [1:11] "ldL2" "ldRX2" "wrss" "ussq" ...
.. ..$ dims: Named int [1:14] 480 480 3 477 2 72 1 1 0 2 ...
.. .. ..- attr(*, "names")= chr [1:14] "N" "n" "p" "nmp" ...
..# pp :Reference class 'merPredD' [package "lme4"] with 18 fields
.. ..$ Lambdat:Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
.. .. .. ..# i : int [1:72] 0 1 2 3 4 5 6 7 8 9 ...
.. .. .. ..# p : int [1:73] 0 1 2 3 4 5 6 7 8 9 ...
.. .. .. ..# Dim : int [1:2] 72 72
.. .. .. ..# Dimnames:List of 2
.. .. .. .. ..$ : NULL
.. .. .. .. ..$ : NULL
.. .. .. ..# x : num [1:72] 0.0761 0.0761 0.0761 0.0761 0.0761 ...
.. .. .. ..# factors : list()
.. ..$ LamtUt :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
.. .. .. ..# i : int [1:960] 0 60 0 60 0 60 0 60 0 60 ...
.. .. .. ..# p : int [1:481] 0 2 4 6 8 10 12 14 16 18 ...
.. .. .. ..# Dim : int [1:2] 72 480
.. .. .. ..# Dimnames:List of 2
.. .. .. .. ..$ : NULL
.. .. .. .. ..$ : NULL
.. .. .. ..# x : num [1:960] 0.0284 0.3935 0.0365 0.5046 0.0284 ...
.. .. .. ..# factors : list()
.. ..$ Lind : int [1:72] 1 1 1 1 1 1 1 1 1 1 ...
.. ..$ Ptr :<externalptr>
.. ..$ RZX : num [1:72, 1:3] 0.124 0.125 0.124 0.125 0.124 ...
.. ..$ Ut :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
.. .. .. ..# i : int [1:960] 0 60 0 60 0 60 0 60 0 60 ...
.. .. .. ..# p : int [1:481] 0 2 4 6 8 10 12 14 16 18 ...
.. .. .. ..# Dim : int [1:2] 72 480
.. .. .. ..# Dimnames:List of 2
.. .. .. .. ..$ : chr [1:72] "2" "4" "6" "8" ...
.. .. .. .. ..$ : NULL
.. .. .. ..# x : num [1:960] 0.373 0.373 0.479 0.479 0.373 ...
.. .. .. ..# factors : list()
.. ..$ Utr : num [1:72] -0.094 -0.018 -0.094 0.058 -0.094 ...
.. ..$ V : num [1:480, 1:3] 0.373 0.479 0.373 0.479 0.451 ...
.. ..$ VtV : num [1:3, 1:3] 92.7 0 0 48.3 48.3 ...
.. ..$ Vtr : num [1:3] 15.738 0.879 3.455
.. ..$ X : num [1:480, 1:3] 1 1 1 1 1 1 1 1 1 1 ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr [1:480] "1" "2" "3" "4" ...
.. .. .. ..$ : chr [1:3] "(Intercept)" "TreatmentExp" "HabitatB"
.. .. ..- attr(*, "assign")= int [1:3] 0 1 2
.. .. ..- attr(*, "contrasts")=List of 2
.. .. .. ..$ Treatment: chr "contr.treatment"
.. .. .. ..$ Habitat : chr "contr.treatment"
.. ..$ Xwts : num [1:480] 0.373 0.479 0.373 0.479 0.451 ...
.. ..$ Zt :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
.. .. .. ..# i : int [1:960] 0 60 0 60 0 60 0 60 0 60 ...
.. .. .. ..# p : int [1:481] 0 2 4 6 8 10 12 14 16 18 ...
.. .. .. ..# Dim : int [1:2] 72 480
.. .. .. ..# Dimnames:List of 2
.. .. .. .. ..$ : chr [1:72] "2" "4" "6" "8" ...
.. .. .. .. ..$ : NULL
.. .. .. ..# x : num [1:960] 1 1 1 1 1 1 1 1 1 1 ...
.. .. .. ..# factors : list()
.. ..$ beta0 : num [1:3] 0 0 0
.. ..$ delb : num [1:3] 0 0 0
.. ..$ delu : num [1:72] -0.05 0.0254 -0.05 0.1008 -0.05 ...
.. ..$ theta : num [1:2] 0.0761 1.0537
.. ..$ u0 : num [1:72] 0 0 0 0 0 0 0 0 0 0 ...
.. ..and 42 methods, of which 30 are possibly relevant:
.. .. b, beta, CcNumer, copy#envRefClass, initialize, initializePtr,
.. .. installPars, L, ldL2, ldRX2, linPred, P, ptr, RX, RXdiag, RXi, setBeta0,
.. .. setDelb, setDelu, setTheta, solve, solveU, sqrL, u, unsc, updateDecomp,
.. .. updateL, updateLamtUt, updateRes, updateXwts
..# optinfo:List of 7
.. ..$ optimizer: chr "Nelder_Mead"
.. ..$ control :List of 3
.. .. ..$ xst : num [1:5] 0.02 0.02 0.0721 0.0421 0.0418
.. .. ..$ xt : num [1:5] 1.00e-05 1.00e-05 3.60e-05 2.11e-05 2.09e-05
.. .. ..$ verbose: int 0
.. ..$ derivs :List of 2
.. .. ..$ gradient: num [1:5] 4.64e-05 9.60e-05 -6.73e-05 -6.46e-05 -1.26e-04
.. .. ..$ Hessian : num [1:5, 1:5] 2.644 -0.247 0.162 -1.023 -0.625 ...
.. ..$ conv :List of 2
.. .. ..$ opt : num 0
.. .. ..$ lme4: list()
.. ..$ feval : num 232
.. ..$ warnings : list()
.. ..$ val : num [1:5] 0.0761 1.0537 -1.2511 1.0118 0.676
You can use the function model.matrix to obtain the fixed-effects design matrix:
model.matrix(fit)
where fit is the object returned by glmer.

Extract all standard errors of coefficients from list of logistic regressions

I want to extract the standard errors from a list of logistic regression models.
This is the logistic regression function, designed this way so i can run more than one analysis at once:
glmfunk <- function(x) glm( ldata$DFREE ~ x , family=binomial)
I run it on a subset of the variables in the dataframe ldata:
glmkort <- lapply(ldata[,c(2,3,5,6,7,8)],glmfunk)
I can extract the coefficients like this:
sapply(glmkørt, "[[", "coefficients")
But how do i extract the standard error of the coefficients? I can't seem to find it in the str(glmkort)?
Here is the str(glmkort) for AGE where i am looking for the standard error:
str(glmkort)
List of 6
$ AGE :List of 30
..$ coefficients : Named num [1:2] -1.17201 -0.00199
.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "x"
..$ residuals : Named num [1:40] -1.29 -1.29 -1.29 -1.29 4.39 ...
.. ..- attr(*, "names")= chr [1:40] "1" "2" "3" "4" ...
..$ fitted.values : Named num [1:40] 0.223 0.225 0.225 0.225 0.228 ...
.. ..- attr(*, "names")= chr [1:40] "1" "2" "3" "4" ...
..$ effects : Named num [1:40] 3.2662 -0.0282 -0.4595 -0.4464 2.042 ...
.. ..- attr(*, "names")= chr [1:40] "(Intercept)" "x" "" "" ...
..$ R : num [1:2, 1:2] -2.64 0 -86.01 14.18
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:2] "(Intercept)" "x"
.. .. ..$ : chr [1:2] "(Intercept)" "x"
..$ rank : int 2
..$ qr :List of 5
.. ..$ qr : num [1:40, 1:2] -2.641 0.158 0.158 0.158 0.159 ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr [1:40] "1" "2" "3" "4" ...
.. .. .. ..$ : chr [1:2] "(Intercept)" "x"
.. ..$ rank : int 2
.. ..$ qraux: num [1:2] 1.16 1.01
.. ..$ pivot: int [1:2] 1 2
.. ..$ tol : num 1e-11
.. ..- attr(*, "class")= chr "qr"
..$ family :List of 12
.. ..$ family : chr "binomial"
.. ..$ link : chr "logit"
.. ..$ linkfun :function (mu)
.. ..$ linkinv :function (eta)
.. ..$ variance :function (mu)
.. ..$ dev.resids:function (y, mu, wt)
.. ..$ aic :function (y, n, mu, wt, dev)
.. ..$ mu.eta :function (eta)
.. ..$ initialize: expression({ if (NCOL(y) == 1) { if (is.factor(y)) y <- y != levels(y)[1L] n <- rep.int(1, nobs) y[weights == 0] <- 0 if (any(y < 0 | y > 1)) stop("y values must be 0 <= y <= 1") mustart <- (weights * y + 0.5)/(weights + 1) m <- weights * y if (any(abs(m - round(m)) > 0.001)) warning("non-integer #successes in a binomial glm!") } else if (NCOL(y) == 2) { if (any(abs(y - round(y)) > 0.001)) warning("non-integer counts in a binomial glm!") n <- y[, 1] + y[, 2] y <- ifelse(n == 0, 0, y[, 1]/n) weights <- weights * n mustart <- (n * y + 0.5)/(n + 1) } else stop("for the binomial family, y must be a vector of 0 and 1's\n", "or a 2 column matrix where col 1 is no. successes and col 2 is no. failures") })
.. ..$ validmu :function (mu)
.. ..$ valideta :function (eta)
.. ..$ simulate :function (object, nsim)
.. ..- attr(*, "class")= chr "family"
..$ linear.predictors: Named num [1:40] -1.25 -1.24 -1.24 -1.24 -1.22 ...
.. ..- attr(*, "names")= chr [1:40] "1" "2" "3" "4" ...
..$ deviance : num 42.7
..$ aic : num 46.7
..$ null.deviance : num 42.7
..$ iter : int 4
..$ weights : Named num [1:40] 0.173 0.174 0.174 0.174 0.176 ...
.. ..- attr(*, "names")= chr [1:40] "1" "2" "3" "4" ...
..$ prior.weights : Named num [1:40] 1 1 1 1 1 1 1 1 1 1 ...
.. ..- attr(*, "names")= chr [1:40] "1" "2" "3" "4" ...
..$ df.residual : int 38
..$ df.null : int 39
..$ y : Named num [1:40] 0 0 0 0 1 0 1 0 0 0 ...
.. ..- attr(*, "names")= chr [1:40] "1" "2" "3" "4" ...
..$ converged : logi TRUE
..$ boundary : logi FALSE
..$ model :'data.frame': 40 obs. of 2 variables:
.. ..$ ldata$DFREE: int [1:40] 0 0 0 0 1 0 1 0 0 0 ...
.. ..$ x : int [1:40] 39 33 33 32 24 30 39 27 40 36 ...
.. ..- attr(*, "terms")=Classes 'terms', 'formula' length 3 ldata$DFREE ~ x
.. .. .. ..- attr(*, "variables")= language list(ldata$DFREE, x)
.. .. .. ..- attr(*, "factors")= int [1:2, 1] 0 1
.. .. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. .. ..$ : chr [1:2] "ldata$DFREE" "x"
.. .. .. .. .. ..$ : chr "x"
.. .. .. ..- attr(*, "term.labels")= chr "x"
.. .. .. ..- attr(*, "order")= int 1
.. .. .. ..- attr(*, "intercept")= int 1
.. .. .. ..- attr(*, "response")= int 1
.. .. .. ..- attr(*, ".Environment")=<environment: 0x017a5674>
.. .. .. ..- attr(*, "predvars")= language list(ldata$DFREE, x)
.. .. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
.. .. .. .. ..- attr(*, "names")= chr [1:2] "ldata$DFREE" "x"
..$ call : language glm(formula = ldata$DFREE ~ x, family = binomial)
..$ formula :Class 'formula' length 3 ldata$DFREE ~ x
.. .. ..- attr(*, ".Environment")=<environment: 0x017a5674>
..$ terms :Classes 'terms', 'formula' length 3 ldata$DFREE ~ x
.. .. ..- attr(*, "variables")= language list(ldata$DFREE, x)
.. .. ..- attr(*, "factors")= int [1:2, 1] 0 1
.. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. ..$ : chr [1:2] "ldata$DFREE" "x"
.. .. .. .. ..$ : chr "x"
.. .. ..- attr(*, "term.labels")= chr "x"
.. .. ..- attr(*, "order")= int 1
.. .. ..- attr(*, "intercept")= int 1
.. .. ..- attr(*, "response")= int 1
.. .. ..- attr(*, ".Environment")=<environment: 0x017a5674>
.. .. ..- attr(*, "predvars")= language list(ldata$DFREE, x)
.. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
.. .. .. ..- attr(*, "names")= chr [1:2] "ldata$DFREE" "x"
..$ data :<environment: 0x017a5674>
..$ offset : NULL
..$ control :List of 3
.. ..$ epsilon: num 1e-08
.. ..$ maxit : num 25
.. ..$ trace : logi FALSE
..$ method : chr "glm.fit"
..$ contrasts : NULL
..$ xlevels : Named list()
..- attr(*, "class")= chr [1:2] "glm" "lm"
$ BECK :List of 30
Here is the data i have used for the example. I shortened it for the purporse of this question:
> ldata
ID AGE BECK IVHX NDRUGTX RACE TREAT SITE DFREE
1 1 39 9.000 3 1 0 1 0 0
2 2 33 34.000 2 8 0 1 0 0
3 3 33 10.000 3 3 0 1 0 0
4 4 32 20.000 3 1 0 0 0 0
5 5 24 5.000 1 5 1 1 0 1
6 6 30 32.550 3 1 0 1 0 0
7 7 39 19.000 3 34 0 1 0 1
8 8 27 10.000 3 2 0 1 0 0
9 9 40 29.000 3 3 0 1 0 0
10 10 36 25.000 3 7 0 1 0 0
11 11 38 18.900 3 8 0 1 0 0
12 12 29 16.000 1 1 0 1 0 0
13 13 32 36.000 3 2 1 1 0 1
14 14 41 19.000 3 8 0 1 0 0
15 15 31 18.000 3 1 0 1 0 0
16 16 27 12.000 3 3 0 1 0 0
17 17 28 34.000 3 6 0 1 0 0
18 18 28 23.000 2 1 0 1 0 0
19 19 36 26.000 1 15 1 1 0 1
20 20 32 18.900 3 5 0 1 0 1
21 21 33 15.000 1 1 0 0 0 1
22 22 28 25.200 3 8 0 0 0 0
23 23 29 6.632 2 0 0 0 0 0
24 24 35 2.100 3 9 0 0 0 0
25 25 45 26.000 3 6 0 0 0 0
26 26 35 39.789 3 5 0 0 0 0
27 27 24 20.000 1 3 0 0 0 0
28 28 36 16.000 3 7 0 0 0 0
29 29 39 22.000 3 9 0 0 0 1
30 30 36 9.947 2 10 0 0 0 0
31 31 37 9.450 3 1 0 0 0 0
32 32 30 39.000 3 1 0 0 0 0
33 33 44 41.000 3 5 0 0 0 0
34 34 28 31.000 1 6 1 0 0 1
35 35 25 20.000 1 3 1 0 0 0
36 36 30 8.000 3 7 0 1 0 0
37 37 24 9.000 1 1 0 0 0 0
38 38 27 20.000 1 1 0 0 0 0
39 39 30 8.000 1 2 1 0 0 1
40 40 34 8.000 3 0 0 1 0 0
Using an example from ?glm
## Dobson (1990) Page 93: Randomized Controlled Trial :
counts <- c(18,17,15,20,10,20,25,13,12)
outcome <- gl(3,1,9)
treatment <- gl(3,3)
glm.D93 <- glm(counts ~ outcome + treatment, family=poisson())
## copy twice to a list to illustrate
lmod <- list(mod1 = glm.D93, mod2 = glm.D93)
Then we could compute them as summary() would, or extract them after calling summary(). The former is far more efficient as you only compute what you want. The latter doesn't rely on knowing how the standard errors are derived.
Compute the standard errors directly
The standard errors can be computed from the variance-covariance matrix of the model. The diagonal of this matrix contains the variances of the coefficients, and the standard errors are simply the square root of these variances. The vcov() extractor function gets the variance-covariance matrix for us and we square root the diagonals with sqrt(diag()):
> lapply(lmod, function(x) sqrt(diag(vcov(x))))
$mod1
(Intercept) outcome2 outcome3 treatment2 treatment3
0.1708987 0.2021708 0.1927423 0.2000000 0.2000000
$mod2
(Intercept) outcome2 outcome3 treatment2 treatment3
0.1708987 0.2021708 0.1927423 0.2000000 0.2000000
Extract them from a call to summary()
Or we can let summary() compute the standard errors (and a lot more), then use lapply() or sapply() to apply an anonymous function that extracts coef(summary(x)) and takes the second column (in which the standard errors are stored).
lapply(lmod, function(x) coef(summary(x))[,2])
Which gives
> lapply(lmod, function(x) coef(summary(x))[,2])
$mod1
(Intercept) outcome2 outcome3 treatment2 treatment3
0.1708987 0.2021708 0.1927423 0.2000000 0.2000000
$mod2
(Intercept) outcome2 outcome3 treatment2 treatment3
0.1708987 0.2021708 0.1927423 0.2000000 0.2000000
whereas sapply() would give:
> sapply(lmod, function(x) coef(summary(x))[,2])
mod1 mod2
(Intercept) 0.1708987 0.1708987
outcome2 0.2021708 0.2021708
outcome3 0.1927423 0.1927423
treatment2 0.2000000 0.2000000
treatment3 0.2000000 0.2000000
Depending on what you wanted to do , you could extract both the coefficients and the standard errors with a single call:
> lapply(lmod, function(x) coef(summary(x))[,1:2])
$mod1
Estimate Std. Error
(Intercept) 3.044522e+00 0.1708987
outcome2 -4.542553e-01 0.2021708
outcome3 -2.929871e-01 0.1927423
treatment2 1.337909e-15 0.2000000
treatment3 1.421085e-15 0.2000000
$mod2
Estimate Std. Error
(Intercept) 3.044522e+00 0.1708987
outcome2 -4.542553e-01 0.2021708
outcome3 -2.929871e-01 0.1927423
treatment2 1.337909e-15 0.2000000
treatment3 1.421085e-15 0.2000000
But you might prefer them separately?

Resources