Stacking lapply results











up vote
0
down vote

favorite












I am using the following code to generate data, and i am estimating regression models across a list of variables (covar1 and covar2). I have also created confidence intervals for the coefficients and merged them together.



I have been examining all sorts of examples here and on other sites, but i can't seem to accomplish what i want. I want to stack the results for each covar into a single data frame, labeling each cluster of results by the covar it is attributable to (i.e., "covar1" and "covar2"). Here is the code for generating data and results using lapply:



##creating a fake dataset (N=1000, 500 at treated, 500 at control group)
#outcome variable
outcome <- c(rnorm(500, mean = 50, sd = 10), rnorm(500, mean = 70, sd = 10))

#running variable
running.var <- seq(0, 1, by = .0001)
running.var <- sample(running.var, size = 1000, replace = T)

##Put negative values for the running variable in the control group
running.var[1:500] <- -running.var[1:500]

#treatment indicator (just a binary variable indicating treated and control groups)
treat.ind <- c(rep(0,500), rep(1,500))

#create covariates
set.seed(123)
covar1 <- c(rnorm(500, mean = 50, sd = 10), rnorm(500, mean = 50, sd = 20))
covar2 <- c(rnorm(500, mean = 10, sd = 20), rnorm(500, mean = 10, sd = 30))
data <- data.frame(cbind(outcome, running.var, treat.ind, covar1, covar2))
data$treat.ind <- as.factor(data$treat.ind)

#Bundle the covariates names together
covars <- c("covar1", "covar2")

#loop over them using a convenient feature of the "as.formula" function
models <- lapply(covars, function(x){
regres <- lm(as.formula(paste(x," ~ running.var + treat.ind",sep = "")), data = d)
ci <-confint(regres, level=0.95)
regres_ci <- cbind(summary(regres)$coefficient, ci)
})
names(models) <- covars
print(models)


Any nudge in the right direction, or link to a post i just haven't come across, is greatly appreciated.










share|improve this question






















  • What is d in the code?
    – m0nhawk
    Nov 21 at 19:32










  • In the lm() call within the lapply(), is d meant to be data? Also, it would help if you could outline the expected output (dimensions and colnames of the expected dataframe)
    – 12b345b6b78
    Nov 21 at 19:33










  • good points above, I'm guessing something like models %>% purrr::map_df(broom::tidy, .id = "covar_id") will get close to what you want
    – Nate
    Nov 21 at 19:35















up vote
0
down vote

favorite












I am using the following code to generate data, and i am estimating regression models across a list of variables (covar1 and covar2). I have also created confidence intervals for the coefficients and merged them together.



I have been examining all sorts of examples here and on other sites, but i can't seem to accomplish what i want. I want to stack the results for each covar into a single data frame, labeling each cluster of results by the covar it is attributable to (i.e., "covar1" and "covar2"). Here is the code for generating data and results using lapply:



##creating a fake dataset (N=1000, 500 at treated, 500 at control group)
#outcome variable
outcome <- c(rnorm(500, mean = 50, sd = 10), rnorm(500, mean = 70, sd = 10))

#running variable
running.var <- seq(0, 1, by = .0001)
running.var <- sample(running.var, size = 1000, replace = T)

##Put negative values for the running variable in the control group
running.var[1:500] <- -running.var[1:500]

#treatment indicator (just a binary variable indicating treated and control groups)
treat.ind <- c(rep(0,500), rep(1,500))

#create covariates
set.seed(123)
covar1 <- c(rnorm(500, mean = 50, sd = 10), rnorm(500, mean = 50, sd = 20))
covar2 <- c(rnorm(500, mean = 10, sd = 20), rnorm(500, mean = 10, sd = 30))
data <- data.frame(cbind(outcome, running.var, treat.ind, covar1, covar2))
data$treat.ind <- as.factor(data$treat.ind)

#Bundle the covariates names together
covars <- c("covar1", "covar2")

#loop over them using a convenient feature of the "as.formula" function
models <- lapply(covars, function(x){
regres <- lm(as.formula(paste(x," ~ running.var + treat.ind",sep = "")), data = d)
ci <-confint(regres, level=0.95)
regres_ci <- cbind(summary(regres)$coefficient, ci)
})
names(models) <- covars
print(models)


Any nudge in the right direction, or link to a post i just haven't come across, is greatly appreciated.










share|improve this question






















  • What is d in the code?
    – m0nhawk
    Nov 21 at 19:32










  • In the lm() call within the lapply(), is d meant to be data? Also, it would help if you could outline the expected output (dimensions and colnames of the expected dataframe)
    – 12b345b6b78
    Nov 21 at 19:33










  • good points above, I'm guessing something like models %>% purrr::map_df(broom::tidy, .id = "covar_id") will get close to what you want
    – Nate
    Nov 21 at 19:35













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am using the following code to generate data, and i am estimating regression models across a list of variables (covar1 and covar2). I have also created confidence intervals for the coefficients and merged them together.



I have been examining all sorts of examples here and on other sites, but i can't seem to accomplish what i want. I want to stack the results for each covar into a single data frame, labeling each cluster of results by the covar it is attributable to (i.e., "covar1" and "covar2"). Here is the code for generating data and results using lapply:



##creating a fake dataset (N=1000, 500 at treated, 500 at control group)
#outcome variable
outcome <- c(rnorm(500, mean = 50, sd = 10), rnorm(500, mean = 70, sd = 10))

#running variable
running.var <- seq(0, 1, by = .0001)
running.var <- sample(running.var, size = 1000, replace = T)

##Put negative values for the running variable in the control group
running.var[1:500] <- -running.var[1:500]

#treatment indicator (just a binary variable indicating treated and control groups)
treat.ind <- c(rep(0,500), rep(1,500))

#create covariates
set.seed(123)
covar1 <- c(rnorm(500, mean = 50, sd = 10), rnorm(500, mean = 50, sd = 20))
covar2 <- c(rnorm(500, mean = 10, sd = 20), rnorm(500, mean = 10, sd = 30))
data <- data.frame(cbind(outcome, running.var, treat.ind, covar1, covar2))
data$treat.ind <- as.factor(data$treat.ind)

#Bundle the covariates names together
covars <- c("covar1", "covar2")

#loop over them using a convenient feature of the "as.formula" function
models <- lapply(covars, function(x){
regres <- lm(as.formula(paste(x," ~ running.var + treat.ind",sep = "")), data = d)
ci <-confint(regres, level=0.95)
regres_ci <- cbind(summary(regres)$coefficient, ci)
})
names(models) <- covars
print(models)


Any nudge in the right direction, or link to a post i just haven't come across, is greatly appreciated.










share|improve this question













I am using the following code to generate data, and i am estimating regression models across a list of variables (covar1 and covar2). I have also created confidence intervals for the coefficients and merged them together.



I have been examining all sorts of examples here and on other sites, but i can't seem to accomplish what i want. I want to stack the results for each covar into a single data frame, labeling each cluster of results by the covar it is attributable to (i.e., "covar1" and "covar2"). Here is the code for generating data and results using lapply:



##creating a fake dataset (N=1000, 500 at treated, 500 at control group)
#outcome variable
outcome <- c(rnorm(500, mean = 50, sd = 10), rnorm(500, mean = 70, sd = 10))

#running variable
running.var <- seq(0, 1, by = .0001)
running.var <- sample(running.var, size = 1000, replace = T)

##Put negative values for the running variable in the control group
running.var[1:500] <- -running.var[1:500]

#treatment indicator (just a binary variable indicating treated and control groups)
treat.ind <- c(rep(0,500), rep(1,500))

#create covariates
set.seed(123)
covar1 <- c(rnorm(500, mean = 50, sd = 10), rnorm(500, mean = 50, sd = 20))
covar2 <- c(rnorm(500, mean = 10, sd = 20), rnorm(500, mean = 10, sd = 30))
data <- data.frame(cbind(outcome, running.var, treat.ind, covar1, covar2))
data$treat.ind <- as.factor(data$treat.ind)

#Bundle the covariates names together
covars <- c("covar1", "covar2")

#loop over them using a convenient feature of the "as.formula" function
models <- lapply(covars, function(x){
regres <- lm(as.formula(paste(x," ~ running.var + treat.ind",sep = "")), data = d)
ci <-confint(regres, level=0.95)
regres_ci <- cbind(summary(regres)$coefficient, ci)
})
names(models) <- covars
print(models)


Any nudge in the right direction, or link to a post i just haven't come across, is greatly appreciated.







r






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 19:30









Jason Schoeneberger

32




32












  • What is d in the code?
    – m0nhawk
    Nov 21 at 19:32










  • In the lm() call within the lapply(), is d meant to be data? Also, it would help if you could outline the expected output (dimensions and colnames of the expected dataframe)
    – 12b345b6b78
    Nov 21 at 19:33










  • good points above, I'm guessing something like models %>% purrr::map_df(broom::tidy, .id = "covar_id") will get close to what you want
    – Nate
    Nov 21 at 19:35


















  • What is d in the code?
    – m0nhawk
    Nov 21 at 19:32










  • In the lm() call within the lapply(), is d meant to be data? Also, it would help if you could outline the expected output (dimensions and colnames of the expected dataframe)
    – 12b345b6b78
    Nov 21 at 19:33










  • good points above, I'm guessing something like models %>% purrr::map_df(broom::tidy, .id = "covar_id") will get close to what you want
    – Nate
    Nov 21 at 19:35
















What is d in the code?
– m0nhawk
Nov 21 at 19:32




What is d in the code?
– m0nhawk
Nov 21 at 19:32












In the lm() call within the lapply(), is d meant to be data? Also, it would help if you could outline the expected output (dimensions and colnames of the expected dataframe)
– 12b345b6b78
Nov 21 at 19:33




In the lm() call within the lapply(), is d meant to be data? Also, it would help if you could outline the expected output (dimensions and colnames of the expected dataframe)
– 12b345b6b78
Nov 21 at 19:33












good points above, I'm guessing something like models %>% purrr::map_df(broom::tidy, .id = "covar_id") will get close to what you want
– Nate
Nov 21 at 19:35




good points above, I'm guessing something like models %>% purrr::map_df(broom::tidy, .id = "covar_id") will get close to what you want
– Nate
Nov 21 at 19:35












1 Answer
1






active

oldest

votes

















up vote
1
down vote













You can use do.call were de second argument is a list (like in here):



do.call(rbind, models)


I made a (possible) improve to your lapply function. This way you can save the estimated parameters and the variables in a data.frame:



models <- lapply(covars, function(x){
regres <- lm(as.formula(paste(x," ~ running.var + treat.ind",sep = "")), data = data)
ci <-confint(regres, level=0.95)
regres_ci <- data.frame(covar=x,param=rownames(summary(regres)$coefficient),
summary(regres)$coefficient, ci)
})

do.call(rbind,models)





share|improve this answer





















  • Thanks! This is concise and gives me what i need. Much appreciated!
    – Jason Schoeneberger
    Nov 21 at 22:38











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419292%2fstacking-lapply-results%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













You can use do.call were de second argument is a list (like in here):



do.call(rbind, models)


I made a (possible) improve to your lapply function. This way you can save the estimated parameters and the variables in a data.frame:



models <- lapply(covars, function(x){
regres <- lm(as.formula(paste(x," ~ running.var + treat.ind",sep = "")), data = data)
ci <-confint(regres, level=0.95)
regres_ci <- data.frame(covar=x,param=rownames(summary(regres)$coefficient),
summary(regres)$coefficient, ci)
})

do.call(rbind,models)





share|improve this answer





















  • Thanks! This is concise and gives me what i need. Much appreciated!
    – Jason Schoeneberger
    Nov 21 at 22:38















up vote
1
down vote













You can use do.call were de second argument is a list (like in here):



do.call(rbind, models)


I made a (possible) improve to your lapply function. This way you can save the estimated parameters and the variables in a data.frame:



models <- lapply(covars, function(x){
regres <- lm(as.formula(paste(x," ~ running.var + treat.ind",sep = "")), data = data)
ci <-confint(regres, level=0.95)
regres_ci <- data.frame(covar=x,param=rownames(summary(regres)$coefficient),
summary(regres)$coefficient, ci)
})

do.call(rbind,models)





share|improve this answer





















  • Thanks! This is concise and gives me what i need. Much appreciated!
    – Jason Schoeneberger
    Nov 21 at 22:38













up vote
1
down vote










up vote
1
down vote









You can use do.call were de second argument is a list (like in here):



do.call(rbind, models)


I made a (possible) improve to your lapply function. This way you can save the estimated parameters and the variables in a data.frame:



models <- lapply(covars, function(x){
regres <- lm(as.formula(paste(x," ~ running.var + treat.ind",sep = "")), data = data)
ci <-confint(regres, level=0.95)
regres_ci <- data.frame(covar=x,param=rownames(summary(regres)$coefficient),
summary(regres)$coefficient, ci)
})

do.call(rbind,models)





share|improve this answer












You can use do.call were de second argument is a list (like in here):



do.call(rbind, models)


I made a (possible) improve to your lapply function. This way you can save the estimated parameters and the variables in a data.frame:



models <- lapply(covars, function(x){
regres <- lm(as.formula(paste(x," ~ running.var + treat.ind",sep = "")), data = data)
ci <-confint(regres, level=0.95)
regres_ci <- data.frame(covar=x,param=rownames(summary(regres)$coefficient),
summary(regres)$coefficient, ci)
})

do.call(rbind,models)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 at 19:46









P. Paccioretti

1155




1155












  • Thanks! This is concise and gives me what i need. Much appreciated!
    – Jason Schoeneberger
    Nov 21 at 22:38


















  • Thanks! This is concise and gives me what i need. Much appreciated!
    – Jason Schoeneberger
    Nov 21 at 22:38
















Thanks! This is concise and gives me what i need. Much appreciated!
– Jason Schoeneberger
Nov 21 at 22:38




Thanks! This is concise and gives me what i need. Much appreciated!
– Jason Schoeneberger
Nov 21 at 22:38


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419292%2fstacking-lapply-results%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo