r - Short code to extract fitted value from tbl_df objects -
i have data set containing groups of data , performed regression on on each group of data. used dplyr regression , tbl_df object results. want extract fitted value vector each group of regression , put them in data frame. used use summarise() extract relevant information conveniently. works scalars. here sample code lapply used extract information , feel kind of cumbersome:
library(dplyr) library(reshape2) df1 = data.frame(type1 = c(rep('a',5),rep('b',5)), x = 1:10, y = 11:20) df1 %>% group_by(type1) %>% do(model = lm(y~x,.)) -> model1 names(model1$model) = model1$type1 lapply(model1$model,function(mod) mod$fit) %>% melt
library(broom) model1 %>% augment(model)
# tibble: 10 x 10 # groups: type1 [2] type1 y x .fitted .se.fit .resid .hat .sigma .cooksd .std.resid <fctr> <int> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> 1 11 1 11 2.482534e-16 3.567051e-19 0.6 3.925229e-16 2.322633e-06 0.001759785 2 12 2 12 1.755417e-16 3.026750e-16 0.3 2.977199e-16 2.730293e-01 1.128776594 3 13 3 13 1.433292e-16 -3.857170e-16 0.2 2.471607e-16 2.263176e-01 -1.345563357 4 14 4 14 1.755417e-16 -1.380180e-16 0.3 3.747906e-16 5.677113e-02 -0.514715401 5 15 5 15 2.482534e-16 2.207032e-16 0.6 3.052655e-16 8.891591e-01 1.088827560 6 b 16 6 16 1.709167e-15 -2.416065e-15 0.6 8.008132e-17 2.248024e+00 -1.731290167 7 b 17 7 17 1.208563e-15 2.359219e-15 0.3 1.824137e-15 3.499565e-01 1.277939838 8 b 18 8 18 9.867878e-16 1.265324e-15 0.2 2.510473e-15 5.138141e-02 0.641132787 9 b 19 9 19 1.208563e-15 5.595623e-17 0.3 2.702016e-15 1.968677e-04 0.030310330 10 b 20 10 20 1.709167e-15 -1.264434e-15 0.6 2.303179e-15 6.157097e-01 -0.906060815
Comments
Post a Comment