I am trying to generate a data frame of intercepts and coefficients for multiple different linear regression models generated from subsets of a data set. I cannot share my data unfortunately but I can explain it using the mtcars set. I am creating a regression model predicting mpg from cyl, hp, and wt for each value of carb.After digging around for a while, I found many examples that do what I want but only for a model with 1 predictor (like mpg~wt). It all falls apart when I add the other terms. This is what I have based my work so far on:https://community.rstudio.com/t/extract-slopes-by-group-broom-dplyr/2751/8Efficient way to extract coefficients from many linear regression lines
This is what I have tried
library(tidyverse);library(broom)df <- mtcarstryme <- df %>% split(.$carb)%>% map(~lm(mpg~cyl+hp+wt,data=.x)) %>% map_df(tidy)
with this result
term estimate std.error statistic p.value1 (Intercept) 46.034633 7.68430306 5.99073626 9.31E-032 cyl 2.650503624 4.14371413 0.63964442 5.68E-013 hp -0.230007961 0.1573354 -1.46189576 2.40E-014 wt -5.231999683 9.23136027 -0.56676368 6.11E-015 (Intercept) 39.84451509 2.95537984 13.48202845 1.03E-056 cyl -0.846094229 0.93995084 -0.90014732 4.03E-017 hp -0.007452737 0.03998485 -0.18638901 8.58E-018 wt -4.133340298 1.42757472 -2.89535829 2.75E-029 (Intercept) 17.50267062 22.13706712 0.79064993 5.74E-0110 cyl NA NA NA NA11 hp NA NA NA NA12 wt -0.3115727 5.73067255 -0.05436931 9.65E-0113 (Intercept) 45.33390978 12.93999647 3.50339429 1.28E-0214 cyl -4.195214198 3.492613 -1.20116778 2.75E-0115 hp 0.029361878 0.04927895 0.59583008 5.73E-0116 wt -1.239041102 1.03937377 -1.19210349 2.78E-0117 (Intercept) 19.7 NaN NaN NaN18 cyl NA NA NA NA19 hp NA NA NA NA20 wt NA NA NA NA21 (Intercept) 15 NaN NaN NaN22 cyl NA NA NA NA23 hp NA NA NA NA24 wt NA NA NA NA
what I want is a table that looks like this:
carb intercept cyl hp wt1 46.034633 2.650503624 -0.230007961 -5.2319996832 39.84451509 -0.846094229 -0.007452737 -4.1333402983 17.50267062 NA NA -0.31157274 45.33390978 -4.195214198 0.029361878 -1.2390411026 19.7 NA NA NA8 15 NA NA NA
I don't know how to bring the value of the grouping variable in. If I can get that added to what I already have I know how to transpose the data into the form I need.
I am updating a bit, as an answer here gave me what I wanted, but is causing an error that I can't seem to fix. the solution uses lmList from the nlme library. The code I am using is this
fit <- lmList(SentLength~Unified_UPPER+Unified_LOWER+GRADE | SentType, data=df, na.omit)
and this is the data I am running it on:SentType SentLength Unified_UPPER Unified_LOWER GRADEJail 0.06578947 0.06666667 0.06666667 3Other 0 6 0 4Other 0 6 6 1Probation 6 0 0 1Other 0 9 0 6Jail 1.41447368 6 0 3Probation 6 0 0 1Probation 6 0 0 1Probation 12 0 0 2Jail 6 16 6 6Prison 36 27 21 5Probation 24 9 0 6Jail 0.23026316 0.5 0 1Jail 0.09868421 0.06666667 0.06666667 1Probation 60 27 21 6Probation 6 1 0 1Probation 24 0 0 3Prison 36 54 36 8Probation 24 9 0 6Probation 6 0 0 1Probation 0 0.06666667 0.06666667 1Probation 24 0 0 3Jail 0.13157895 1 0.06666667 1Jail 0.09868421 1 0.1 1Prison 42 60 48 8Probation 6 0 0 2Other 0 0 0 1Prison 6 6 6 1Prison 6 6 6 1Other 0 16 9 7
I get this error:Error in na.fail.default(data) : missing values in objectdespite no missing values in the data. Can anyone help with this?