0
votes

I have a data.frame and a list of observation http://imgur.com/7hL2bDC

and a list of observation times http://imgur.com/w1P0qsg

and I want to create a list with the linear regression line slopes from each column of that data frame and the observation times.

If i was doing it all manually, I'd do:

raw_slope <- lm(score1$AKR ~ obs)

slope <- raw_slope$coefficients[[2]]

and then "slope" is the value I want. I'm trying to understand how i can run something and spit out the slope for each of the 19 company symbols in my data.frame.

I'm kind of a novice at R but I'm trying to learn it, I appreciate any help pointing me in the right direction.

EDIT2: I ran lm(score1$... ~ obs) for each of the rows manually so i know the calculations should all work out.

EDIT: I tried the suggestion from the comments

listslope<-lapply(score1, function(x) coef(lm(x~obs))[2])

and am getting

Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 0 (non-NA) cases

I'm going to try and figure out what's causing the issue, but I wanted to update my post and say thanks.

1

1 Answers

1
votes
lapply(score1, function(x) coef(lm(x~obs))[2])

Should give it to you in a list.

Use sapply if you'd prefer a vector.