group_by
の後のすべてのグループに対して線形回帰を行い、モデル係数をリスト列に保存し、 'unnest'を使用してリスト列を展開します。
ここでは、mtcars
データセットを例として使用します。
注:do' here, because
house :: tidy 'を使用したいすべてのモデルでは機能しません。
mtcars %>% group_by(cyl) %>%
do(model=lm(mpg~wt+hp, data=.)) %>%
mutate(coefs = list(summary(model)$coefficients)) %>%
unnest()
私はこのようなものが欲しい。
cyl term Estimate Std. Error t value Pr(>|t|)
4 (Intercept) 36.9083305 2.19079864 16.846975 1.620660e-16
4 wt -2.2646936 0.57588924 -3.932516 4.803752e-04
4 hp -0.0191217 0.01500073 -1.274718 2.125285e-01
6.......
6......
........
私は以下のようなエラーがあります:
Error: All nested columns must have the same number of elements.
誰もこの問題を解決するのを助けることができますか?私は何度も試した後にそれを理解することができませんでした...