[R] Help w/ variable names in loop with lmer
David Crow
david.crow at cide.edu
Sun Mar 29 23:59:59 CEST 2015
Hi, R users-
I'm estimating random effects models with cross-level interactions; I want
to interact each of a vector of level-1 variables with each of a vector of
level-2 variables. Here's the code:
====================
#create data frame with level-1 variables
k <- as.data.frame(cbind(robo, asalto, secuestro, asesinato))
#create data frame with level-2 variables
l <- as.data.frame(cbind(IDH_IDH, IDH_ingpc, eco_pb, IM_indice, tasa_robo,
hom_tasa, totdelitos1, totdelitos2, total, pri, pan, prd))
#get cross-level interactions
for (i in 1:length(k)) {
for (j in 1:length(l)) {
print(summary(lmer(hrprotcrim ~ k[,i]*l[,j] + (k[,i] | Municipio))))
}
}
======================
The code works and produces 48 (4 level-1 x 12 level-2) sets of output.
The problem is, the output is illegible because instead of the variable
names, I get the indices:
[output]
==================================
Linear mixed model fit by REML ['lmerMod']
Formula: hrprotcrim ~ k[, i] * l[, j] + (k[, i] | Municipio)
REML criterion at convergence: 8801.4
Scaled residuals:
Min 1Q Median 3Q Max
-2.4447 -0.7017 -0.2639 0.6766 3.0835
Random effects:
Groups Name Variance Std.Dev. Corr
Municipio (Intercept) 1.067868 1.0334
k[, i] 0.005387 0.0734 1.00
Residual 2.976150 1.7252
Number of obs: 2163, groups: Municipio, 180
Fixed effects:
Estimate Std. Error t value
(Intercept) 2.710847 0.101715 26.651
k[, i] -0.056720 0.355802 -0.159
l[, j] 0.002701 0.002289 1.180
k[, i]:l[, j] 0.006510 0.006340 1.027
Correlation of Fixed Effects:
(Intr) k[, i] l[, j]
k[, i] -0.048
l[, j] -0.514 0.028
k[,i]:l[,j] 0.034 -0.566 -0.072
==================================
Two questions:
1) How can I get variable names instead of indices in the above output
2) How can I estimate this with "mapply" instead of the double loop?
Here's the code for "mapply"
M4 <- mapply(function(k,l){summary(lmer(hrprotcrim ~ k*l + (k |
Municipio)))})
And here's what I get:
list()
I'd be grateful for any pointers.
Best,
David
--
Personal Web site:
http://investigadores.cide.edu/crow/
Web site for México, las Américas y el Mundo:
http://mexicoyelmundo.cide.edu/
====================================
David Crow, Ph.D.
Profesor-Investigador/Assistant Professor
Director General, *Las Américas y el Mundo*
División de Estudios Internacionales
Carretera México-Toluca 3655
Col. Lomas de Santa Fe 01210 México, D.F.
Tel.: 5727-9800, ext. 2152
Fax: 5727-9872
====================================
Conmutador: 5727-98-00 Lada sin costo: 01 800 021 2433 (CIDE) |©
[[alternative HTML version deleted]]
More information about the R-help
mailing list