Re: [R] Help w/ variable names in loop with lmer

2015-03-29 Thread Ben Bolker
David Crow  cide.edu> writes:

> 
> 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)
> 

[snip]

> 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 would suggest appropriate use of ?reformulate

Assume dd is a data frame containing all variables

lev1vars <- c("robo", "asalto", "secuestro", "asesinato")
lev2vars <- c("IDH_IDH", "IDH_ingpc", ...)
ffun <- function(L1var,L2var) {
ff <- reformulate(paste(L1var,L2var,sep="*"),
  paste(L1var,"Municipio",sep="|"),
  response="hrprotcrim")
environment(ff) <- parent.frame()  ## trickiness
return(summary(lmer(ff,data=dd)))
}
mapply(ffun,lev1vars,lev2vars)

?

If you had given a reproducible example I could have tested this ...

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Help w/ variable names in loop with lmer

2015-03-29 Thread David Crow
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:
 GroupsNameVariance 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]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.