I have a dataset which is like this.
library(dplyr)
set.seed(505)
tempdf1 <-
data.frame(
y = rnorm(400),
x1 = rnorm(400),
x2 = rnorm(400),
x3 = sample(1:5, 40, replace = TRUE),
out = rep(1:2, each = 200),
imp = rep(1:4, each = 20)
)
There are two different outcomes as indicated in the column out, {1,2}.
I want to fit the model separately for these two outcomes.
So I use lapply like this.
tempdf2 <- split(tempdf1, tempdf1$out)
lapply(tempdf2, function(df) {
df %>%
group_by(imp) %>%
do(model = lm(y ~ x1 + x2, data = .)) %>%
as.list() %>%
.[[-1]]
})
I have column x3. This is a factor, with 5 values, {1,2,3,4,5}. Now I want to fit the same model above (2 different outcomes, 2 seperate models lapply(tempdf2, function(df) { df %>% group_by(imp) %>%....
When out = 1
Case1) Exclude x3 value 5 and fit the model where x3 values {1,2,3,4}: out = 1
lapply(tempdf2,
function(df) {
df %>%
group_by(imp) %>%....`
Case2) Exclude x3 value 4 and fit the model where x3 values {1,2,3,5}: out = 1
lapply(tempdf2,
function(df) {
df %>%
group_by(imp) %>%....`
Case3) Exclude x3 value 3 and fit the model where x3 values {1,2,4,5}: out = 1
lapply(tempdf2,
function(df) {
df %>%
group_by(imp) %>%....`
Case4) Exclude x3 value 2 and fit the model where x3 values {1,3,4,5}: out = 1
lapply(tempdf2,
function(df) {
df %>%
group_by(imp) %>%....`
Case5) Exclude x3 value 1 and fit the model where x3 values {2,3,4,5}: out = 1
lapply(tempdf2,
function(df) {
df %>%
group_by(imp) %>%....`
Now repeat the above process where out=2
This is where I am struggling and I need help.
I dont know how to include a second inner lapply loop that will fit the above model only on subsets of x3 after excluding rows with one level of x3 , one at a time.
Also I like to know which model corresponds to which excluded rows of x3 and which out values {1,2}, so that I can keep track of the results. Thanks in advance for any help.