Aggregate linear regression

天大地大妈咪最大 提交于 2019-12-01 20:26:37

You can also do some magic with the base lm to do it all at once:

coef(lm(game ~ pts*name - pts, data=gamelogs))[3:4]
coef(lm(game ~ pts:name + name, data=gamelogs))[3:4]
#pts:nameplayer1 pts:nameplayer2 
#    -0.42857143      0.08241758 

As a data.frame:

data.frame(slope=coef(lm(game ~ pts*name - pts, data=gamelogs))[3:4])
#                      slope
#pts:nameplayer1 -0.42857143
#pts:nameplayer2  0.08241758

See here for some further explanation of the modelling in the lm call:

https://stat.ethz.ch/R-manual/R-devel/library/stats/html/formula.html
http://faculty.chicagobooth.edu/richard.hahn/teaching/FormulaNotation.pdf#2

In this case pts*name expands to pts + name + pts:name which when removing - pts means it is equivalent to pts:name + name

library nlme has a function for this as well, lmList

library(nlme)
coef(lmList(game ~ pts | name, gamelogs))
#        (Intercept)         pts
# player1    7.714286 -0.42857143
# player2    4.230769  0.08241758

You could do

s <- split(gamelogs, gamelogs$name)

vapply(s, function(x) lm(game ~ pts, x)[[1]][2], 1)
#     player1     player2 
# -0.42857143  0.08241758 

or

do.call(rbind, lapply(s, function(x) coef(lm(game ~ pts, x))[2]))
#                 pts
# player1 -0.42857143
# player2  0.08241758

Or if you want to use dplyr, you can do

library(dplyr)

models <- group_by(gamelogs, name) %>% 
    do(mod = lm(game ~ pts, data = .))

cbind(
    name = models$name, 
    do(models, data.frame(slope = coef(.$mod)[2]))
)
#      name       slope
# 1 player1 -0.42857143
# 2 player2  0.08241758
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!