如何一次创建多个线性模型并将系数放入新矩阵中? [英] how to create many linear models at once and put the coefficients into a new matrix?

查看:25
本文介绍了如何一次创建多个线性模型并将系数放入新矩阵中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 365 列.在每一列中,我有 60 个值.我需要知道每列(斜率或线性系数)随时间的变化率.我创建了一个通用列作为从 1:60 开始的一系列数字,以表示 60 个相应的时间间隔.我想使用具有 365 列数据中的每一列的通用时间戳列创建 356 个线性回归模型.

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.

推荐答案

首先,从统计学角度来看,这可能不是分析时态数据的最佳方法.虽然,关于您提出的方法,构建一个循环来获得这个非常简单:

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
} 

这篇关于如何一次创建多个线性模型并将系数放入新矩阵中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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