总体线性回归 [英] Aggregate linear regression

查看:125
本文介绍了总体线性回归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我是R的新手,但是我有一个包含多个玩家游戏记录的数据框.我正在尝试获取每个玩家在其所有游戏中的得分的斜率系数.我已经看到aggregate可以使用sumaverage之类的运算符,并且从线性回归中获取系数也非常简单.如何结合这些?

Sorry I am quite new to R, but I have a dataframe with gamelogs for multiple players. I am trying to get the slope coefficient for each player's points over all of their games. I have seen that aggregate can use operators like sum and average, and getting coefficients off of a linear regression is pretty simple as well . How do I combine these?

a <- c("player1","player1","player1","player2","player2","player2")
b <- c(1,2,3,4,5,6)
c <- c(15,12,13,4,15,9)
gamelogs <- data.frame(name=a, game=b, pts=c)

我希望这成为:

   name    pts slope
player1       -.4286
player2       .08242    

推荐答案

您还可以使用基本的lm做一些魔术,一次完成所有操作:

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 

作为data.frame:

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

有关lm调用中建模的更多说明,请参见此处:

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

在这种情况下,pts*name扩展为pts + name + pts:name,这在删除- pts时表示它等同于pts:name + name

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

这篇关于总体线性回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆