looping through a list of independent variables

Welcome to the forum for R2MLwiN users. Feel free to post your question about R2MLwiN here. The Centre for Multilevel Modelling take no responsibility for the accuracy of these posts, we are unable to monitor them closely. Do go ahead and post your question and thank you in advance if you find the time to post any answers!

Go to R2MLwiN: Running MLwiN from within R >> http://www.bris.ac.uk/cmm/software/r2mlwin/
Post Reply
adeldaoud
Posts: 63
Joined: Sat Aug 15, 2015 4:00 pm

looping through a list of independent variables

Post by adeldaoud »

Hi

Has anyone figured out how to loop/iterate through a list of independent variables (and dependent variables)? Preferably with the doParrallel package, as Chris suggested here https://www.cmm.bristol.ac.uk/forum/vie ... 43d352de0c

Suppose I want to iterate through the bang data with the following independent variable separately

Code: Select all

# (1) settings
library(doParallel)
library(R2MLwiN)
data(bang)

cl <- makeCluster(3)
registerDoParallel(cl) # register the cores


# this gives obviously an error since they are objects in the bang data. But I want them to eventually be part of the formula object defined below.
Focal_Independent_variables <- list(urban, age, hindu)


# (2) define the models to be passed to R2mlwin via doParallel
flist<-list(
  list(logit(use, cons) ~ 1 + Focal_Independent_variables  + (1 | district), D = "Binomial", estoptions = list(EstM = 0), data = bang))




result <- foreach(i=1:3, .packages="R2MLwiN", .errorhandling=c('pass')) %dopar% {
  do.call(runMLwiN, flist[[i]])
}
Essentially, the question is how can I combine various parts of a formula objects of R2mlwin?
ChrisCharlton
Posts: 1351
Joined: Mon Oct 19, 2009 10:34 am

Re: looping through a list of independent variables

Post by ChrisCharlton »

One possibility would be to build the required formulae as strings and then converting them into R formulae with the as.formula function (see https://stat.ethz.ch/R-manual/R-devel/l ... rmula.html). An example of this would be as follows:

Code: Select all

# (1) settings
library(doParallel)
library(R2MLwiN)
data(bang)

cl <- makeCluster(3)
registerDoParallel(cl) # register the cores


# this gives obviously an error since they are objects in the bang data. But I want them to eventually be part of the formula object defined below.
Focal_Independent_variables <- c("urban", "age", "hindu")


# (2) define the models to be passed to R2mlwin via doParallel
flist<-list()
for (i in 1:length(Focal_Independent_variables)) {
    flist[[i]] <- list(as.formula(paste0("logit(use, cons) ~ 1 +", Focal_Independent_variables[i], "+ (1 | district)")), D = "Binomial", estoptions = list(EstM = 0), data = bang)
}

result <- foreach::foreach(i=1:3, .packages="R2MLwiN", .errorhandling=c('pass')) %dopar% {
  do.call(runMLwiN, flist[[i]])
}

stopCluster(cl)

Code: Select all

> result
[[1]]

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- 
MLwiN (version: 2.36)  multilevel model (Binomial) 
          N min     mean max N_complete min_complete mean_complete max_complete
district 60   3 47.78333 173         60            3      47.78333          173
Estimation algorithm:  IGLS MQL1        Elapsed time : 0.14s 
Number of obs:  2867 (from total 2867)        The model converged after 5 iterations.
Log likelihood:      NA 
Deviance statistic:  NA 
--------------------------------------------------------------------------------------------------- 
The model formula:
logit(use, cons) ~ 1 + urban + (1 | district)
Level 2: district     Level 1: l1id      
--------------------------------------------------------------------------------------------------- 
The fixed part estimates:  
                Coef.   Std. Err.       z    Pr(>|z|)         [95% Conf.   Interval] 
Intercept    -0.65354     0.07607   -8.59   8.574e-18   ***     -0.80263    -0.50445 
urbanUrban    0.67291     0.09471    7.10   1.204e-12   ***      0.48728     0.85854 
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1  
--------------------------------------------------------------------------------------------------- 
The random part estimates at the district level: 
                  Coef.   Std. Err. 
var_Intercept   0.19017     0.05475 
--------------------------------------------------------------------------------------------------- 
The random part estimates at the l1id level: 
                Coef.   Std. Err. 
var_bcons_1   1.00000     0.00000 
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- 

[[2]]

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- 
MLwiN (version: 2.36)  multilevel model (Binomial) 
          N min     mean max N_complete min_complete mean_complete max_complete
district 60   3 47.78333 173         60            3      47.78333          173
Estimation algorithm:  IGLS MQL1        Elapsed time : 0.12s 
Number of obs:  2867 (from total 2867)        The model converged after 4 iterations.
Log likelihood:      NA 
Deviance statistic:  NA 
--------------------------------------------------------------------------------------------------- 
The model formula:
logit(use, cons) ~ 1 + age + (1 | district)
Level 2: district     Level 1: l1id      
--------------------------------------------------------------------------------------------------- 
The fixed part estimates:  
               Coef.   Std. Err.       z    Pr(>|z|)         [95% Conf.   Interval] 
Intercept   -0.47850     0.07741   -6.18   6.361e-10   ***     -0.63023    -0.32678 
age          0.01331     0.00429    3.10    0.001941   **       0.00489     0.02173 
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1  
--------------------------------------------------------------------------------------------------- 
The random part estimates at the district level: 
                  Coef.   Std. Err. 
var_Intercept   0.23992     0.06435 
--------------------------------------------------------------------------------------------------- 
The random part estimates at the l1id level: 
                Coef.   Std. Err. 
var_bcons_1   1.00000     0.00000 
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- 

[[3]]

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- 
MLwiN (version: 2.36)  multilevel model (Binomial) 
          N min     mean max N_complete min_complete mean_complete max_complete
district 60   3 47.78333 173         60            3      47.78333          173
Estimation algorithm:  IGLS MQL1        Elapsed time : 0.14s 
Number of obs:  2867 (from total 2867)        The model converged after 5 iterations.
Log likelihood:      NA 
Deviance statistic:  NA 
--------------------------------------------------------------------------------------------------- 
The model formula:
logit(use, cons) ~ 1 + hindu + (1 | district)
Level 2: district     Level 1: l1id      
--------------------------------------------------------------------------------------------------- 
The fixed part estimates:  
                Coef.   Std. Err.       z    Pr(>|z|)         [95% Conf.   Interval] 
Intercept    -0.53815     0.07835   -6.87   6.484e-12   ***     -0.69171    -0.38459 
hinduHindu    0.39805     0.11845    3.36   0.0007779   ***      0.16590     0.63021 
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1  
--------------------------------------------------------------------------------------------------- 
The random part estimates at the district level: 
                  Coef.   Std. Err. 
var_Intercept   0.23016     0.06220 
--------------------------------------------------------------------------------------------------- 
The random part estimates at the l1id level: 
                Coef.   Std. Err. 
var_bcons_1   1.00000     0.00000 
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
adeldaoud
Posts: 63
Joined: Sat Aug 15, 2015 4:00 pm

Re: looping through a list of independent variables

Post by adeldaoud »

Sorry for the late reply. This works perfectly.

Thanks Chris
Post Reply