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)
I want this to become:
name pts slope
player1 -.4286
player2 .08242
You can also do some magic with the base
lm
to do it all at once:As a
data.frame
: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 topts + name + pts:name
which when removing- pts
means it is equivalent topts:name + name
You could do
or
Or if you want to use
dplyr
, you can dolibrary
nlme
has a function for this as well,lmList