0

I have 365 columns. In each column I have 60 values. I need to know the rate of change over time for each column (slope or linear coefficient). I created a generic column as a series of numbers from 1:60 to represent the 60 corresponding time intervals. I want to create 356 linear regression models using the generic time stamp column with each of the 365 columns of data.

In other words, I have many columns and I would like to create many linear regression models at once, extract the coefficients and put those coefficients into a new matrix.

  • 2
    It's possible to do this if someone is holding a gun to your head, but it is so statistically dirty that you would need to go take a shower and scrub yourself with turpentine soap. – IRTFM Nov 14 '14 at 04:52
  • I am getting the coefficients of each night's change in water table (recharge when ET is negligible) in order to calculate evapotranspiration using the White Method. – MelissaThaw Nov 19 '14 at 05:21
  • Perhaps if you analyze the coefficients as a correlated time series this might make sense. The way it was described made me think you were on a search for "significant" time points". – IRTFM Nov 19 '14 at 05:52
  • Thanks for your input - I don't want to do bad statistics. – MelissaThaw Nov 20 '14 at 06:44

2 Answers2

0

First of all, statistically this might not be the best possible approach to analyse temporal data. Although, regarding the approach you propose, it is very simple to build a loop to obtain this:

Coefs <- matrix(,ncol(Data),2)#Assuming your generic 1:60 column is not in the same object
for(i in 1:ncol(Data)){
Coefs[i,] <- lm(Data[,i]~GenericColumn)$coefficients
} 
LeoRJorge
  • 474
  • 1
  • 5
  • 13
  • Thank you! I used this and it looks like it works. I tried this one because it seemed simple enough for me to understand. – MelissaThaw Nov 19 '14 at 05:16
0

Here's a way to do it:

# Fake data
dat = data.frame(x=1:60, y1=rnorm(60), y2=rnorm(60), 
                 y3=rnorm(60))

t(sapply(names(dat)[-1], function(var){
   coef(lm(dat[,var] ~ x, data=dat))
}))

   (Intercept)            x
y1  0.10858554 -0.004235449
y2 -0.02766542  0.005364577
y3  0.20283168 -0.008160786

Now, where's that turpentine soap?

eipi10
  • 91,525
  • 24
  • 209
  • 285