I am having troubles with using subset which involves lists in my linear model. One of two things happen, when I use lapply I get object 'x' not found. When I use mapply I get that object 'y' not found. For some reason neither variable is working in subset.
Here are my two approaches:
library(faraway)
data(savings)
form <- "sr ~ pop75 + dpi + ddpi + pop15"
#approach one
lapply(label_res, function(x){
lm(form, savings, subset = (!row.names(savings) %in% x))
})
#output does not change for all linear models
#approach two
form_list<-rep(form, 4) %>% as.list()
mapply(function(x, y)
{lm(x, savings, subset=(!row.names(savings) %in% y))}, form_list, label_res)
>Error in row.names(savings) %in% y : object 'y' not found
However this does work:
library(faraway)
data(savings)
form <- "sr ~ pop75 + dpi + ddpi + pop15"
#approach one
lapply(label_res, function(x){subset(savings, !row.names(savings) %in% x) %>%
lm(form, .)
})
Here s label_res:
list(pop75 = c("Ireland", "Japan"), dpi = c("Ireland", "Sweden",
"United States"), ddpi = "Libya", pop15 = c("Japan", "Libya"))