how to create many linear models at once and put the coefficients into a new matrix?

后端 未结 2 1446
闹比i
闹比i 2021-01-17 02:10

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

2条回答
  •  孤独总比滥情好
    2021-01-17 02:49

    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
    } 
    

提交回复
热议问题