I did create some regression models that for each country in my dataset. However, with a lot of countries this is time-exhausting and not efficient at all. I was thinking of creating a list with country names and itinerate through it with a for loop in order to create the regression models for each country. The models should then be saved in a list of regression models. However, I didn't manage to do it since the different models need seperate names (e.g. model1, model2, and so forth). Does someone know a solution to that problem and to make it more efficient?
Here is an example of three regression models from the countries:
countries <- list("Algeria", "Egypt", "Iraq")
model1 <- glm(family_role_recoded ~ urban_rural,
family=binomial(link='logit'),
subset = (country == "Algeria" &
urban_rural != "Refugee camp"),
data=dataset)
model2 <- glm(family_role_recoded ~ urban_rural,
family=binomial(link='logit'),
subset = (country == "Egypt" &
urban_rural != "Refugee camp"),
data=dataset)
model3 <- glm(family_role_recoded ~ urban_rural,
family=binomial(link='logit'),
subset = (country == "Iraq" &
urban_rural != "Refugee camp"),
data=dataset)
regression_models <- list(model1, model2, model3)
I appreciate any ideas!
Best regards Nicolas
CodePudding user response:
You can create all your models in a list with lapply
countries <- list("Algeria", "Egypt", "Iraq")
regression_models <- lapply(countries, function(x) {
glm(family_role_recoded ~ urban_rural,
family=binomial(link='logit'),
subset = (country == x &
urban_rural != "Refugee camp"),
data=dataset)
})
And then you can get your values out with
regression_models[[1]]
regression_models[[2]]
...etc
For a more "tidyverse" approach, you can use purrr:map
rather than lapply
For example this works with mtcars
gears <- list(3,4,5)
lapply(gears, function(x) {glm(mpg~wt, data=mtcars, subset= gear==x)})
CodePudding user response:
I hope it does work. I used the first 25 rows by using dput(), but stackovervlow keeps saying me that the code appears to contain code that is not properly formatted as code.
structure(list(country = c("Algeria", "Algeria", "Algeria", "Algeria",
"Algeria", "Algeria", "Algeria", "Algeria", "Algeria", "Algeria",
"Egypt", "Egypt", "Egypt", "Egypt", "Egypt", "Egypt",
"Egypt", "Egypt", "Egypt", "Egypt", "Iraq", "Iraq",
"Iraq", "Iraq", "Iraq"), urban_rural = structure(c(2L,
2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L), .Label = c("Rural", "Urban",
"Refugee camp"), class = "factor"), age = c(41L, 36L, 33L, 26L,
28L, 33L, 31L, 45L, 70L, 18L, 23L, 20L, 24L, 44L, 38L, 39L, 23L,
45L, 26L, 54L, 26L, 22L, 33L, 62L, 18L), education = c("Preparatory/Basic",
"Preparatory/Basic", "Secondary", "Elementary", "Mid-level diploma/professional or technical",
"Secondary", "Elementary", "Elementary", "Elementary", "Secondary",
"BA", "Secondary", "BA", "Preparatory/Basic", "Elementary", "BA",
"Secondary", "BA", "Preparatory/Basic", "Secondary", "Secondary",
"Preparatory/Basic", "Preparatory/Basic", "Preparatory/Basic",
"Secondary"), family_role = structure(c(1L, 2L, 2L, 1L, 2L, 2L,
2L, 3L, 2L, 3L, 2L, 3L, 1L, 3L, 2L, 3L, 3L, 2L, 2L, 4L, 2L, 2L,
3L, 1L, 3L), .Label = c("I strongly agree", "I agree", "I disagree",
"I strongly disagree", "Don't know", "Refused to answer"), class = "factor"),
religious = structure(c(2L, 1L, 2L, 2L, 3L, 2L, 2L, 3L, 1L,
3L, 3L, 1L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
3L), .Label = c("Religious", "Somewhat religious", "Not religious"
), class = "factor"), gender = c("Male", "Female", "Female",
"Male", "Male", "Male", "Male", "Male", "Male", "Male", "Female",
"Male", "Male", "Female", "Male", "Female", "Female", "Male",
"Female", "Female", "Male", "Male", "Male", "Female", "Male"
), education_cat = structure(c(2L, 2L, 3L, 2L, 3L, 3L, 2L,
2L, 2L, 3L, 4L, 3L, 4L, 2L, 2L, 4L, 3L, 4L, 2L, 3L, 3L, 2L,
2L, 2L, 3L), .Label = c("No formal education", "Basic education",
"Secondary education", "Higher education"), class = "factor"),
family_role_recoded = structure(c(2L, 2L, 2L, 2L, 2L, 2L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L,
2L, 1L, 2L, 1L), .Label = c("Disagree/strongly disagree",
"Agree/strongly agree", "Don't know"), class = "factor")), row.names = c(NA,
25L), class = "data.frame")