I am trying to perform multiple regression analyses and then extract the coefficients to store each models results together as one dataframe. The regression works perfectly, however afterwards I am left with a matrix object that seemingly cannot be converted to a dataframe. I have tried multiple combinations as.data.frame()
rbind()
rbindlist
melt()
and cbind()
, but nothing seems to mitigate this problem.
Below is an example using the mtcars dataset of the regressions I perform via the sapply
function. I intend for the result (df) to be just a dataframe version of the output I currently have, except this output cannot be manipulated properly as it is in matrix form, not a dataframe. Wrapping this output with as.data.frame()
merely produces a list object.
var_list <- mtcars %>% select(cyl,wt) %>% names()
iterate <- sapply(var_list,function(k){
mod <- lm(data=mtcars, paste("mpg ~ ",k,":factor(drat)",sep = ""))
rbind(coef(mod))
})
Is there any way to store sapply
regression coefficient results in one dataframe? I have seen it suggested elsewhere that similar issues may emerge centered around the "attributes" of the created objects, but it is unclear how to resolve this.
EDIT
My intention is to perform the above within another sapply
function, with another list of outcome variables within the regression. I will then create ggplot
graphs for each outcome variable, using the data for each regression. I would therefore need something like the following:
library(ggplot2)
library(tidyverse)
outcome_var_list = mtcars %>% select(qsec,hp) %>% names()
var_list = mtcars %>% select(cyl,wt) %>% names()
iterate <- sapply(outcome_var_list,function(x){(sapply(var_list,function(k){
mod <- lm(data=mtcars, paste(x," ~ ",k,":factor(drat)",sep = ""))
mod_df = as.data.frame(coef(mod)))
})
#using rownames and coefficient as placeholder names for the actual column names
mod_df %>% ggplot(aes(x = rownames(mod_df), y= coefficient))
})
Yet this does not work because I cannot return a df within the sapply function. Am I approaching this the wrong way? Is there an alternative way to perform the regression for a list of independent variables WITHIN a list of dependent variables, extract the data and generate all the plots?
As IRTFM said, using as.data.frame
(or indeed as_tibble
) converts the matrix into a dataframe.