创建OLS Beta参数估计值的滚动平均值

发布于 2025-01-27 23:59:27 字数 174 浏览 2 评论 0原文

我正在尝试创建一个从单个数据集的OLS Beta参数估算的滚动平均值的时间序列图。我需要用OLS回归数据的第1-36行,找到Beta参数估计,然后以2-37行再次进行此操作,依此类推。完成此操作后,我需要在图中绘制这些点。我找不到一种自动化此过程的方法,以找到每个数据小节的OLS Beta估计值。有人有想法吗?

谢谢!

I am trying to create a time series graph of the rolling average of the OLS Beta parameter estimates from a single data set. I need to regress rows 1-36 of the data with OLS, find the Beta parameter estimate, and then do so again with rows 2-37, and so on. Once that is done, I need to plot these points in a graph. I can't find a way to automate this process of finding the OLS Beta estimates for each subsection of data. Does anyone have any ideas?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的绿洲 2025-02-03 23:59:27

这是一个快速的例子,但是我可以澄清一下您是否看到了您的数据示例?

假设您有一个1000行帧df,带有结果y和predivor x,并且您希望在每个36行滑动窗口(行1-36、2-37、3-38等)。首先,运行964个回归模型并保存系数。

coefs = do.call(rbind, lapply(36:nrow(df),\(i) lm(y~x,data=df[(i-36):i])$coef))

将它们添加到数据中。表

df[36:nrow(df),c("intercept","beta"):=list(coefs[,1], coefs[,2])]

输出:

                x           y  intercept       beta
   1: -0.56047565 -0.99579872         NA         NA
   2: -0.23017749 -1.03995504         NA         NA
   3:  1.55870831 -0.01798024         NA         NA
   4:  0.07050839 -0.13217513         NA         NA
   5:  0.12928774 -2.54934277         NA         NA
  ---                                              
 996: -0.08997520  0.07664366 0.06368493 0.16388413
 997:  1.07051604  0.25516476 0.11283476 0.20039460
 998: -1.35110039  0.27744682 0.06146300 0.08411488
 999: -0.52261670  0.53685602 0.08775808 0.08424470
1000: -0.24919068 -0.46048557 0.03574618 0.07458988

输入:

library(data.table)
set.seed(123)
df = data.table(x=rnorm(1000), y=rnorm(1000))

Here is a quick example, but I could perhaps clarify if I saw an example of your data?

Let's say you have a 1000 row frame df, with outcome y and predictor x, and you want the intercept and slope of a regression done in every 36-row sliding window (rows 1-36, 2-37, 3-38, etc). First, run the 964 regression models and save the coefficients.

coefs = do.call(rbind, lapply(36:nrow(df),\(i) lm(y~x,data=df[(i-36):i])$coef))

Add them to the data.table

df[36:nrow(df),c("intercept","beta"):=list(coefs[,1], coefs[,2])]

Output:

                x           y  intercept       beta
   1: -0.56047565 -0.99579872         NA         NA
   2: -0.23017749 -1.03995504         NA         NA
   3:  1.55870831 -0.01798024         NA         NA
   4:  0.07050839 -0.13217513         NA         NA
   5:  0.12928774 -2.54934277         NA         NA
  ---                                              
 996: -0.08997520  0.07664366 0.06368493 0.16388413
 997:  1.07051604  0.25516476 0.11283476 0.20039460
 998: -1.35110039  0.27744682 0.06146300 0.08411488
 999: -0.52261670  0.53685602 0.08775808 0.08424470
1000: -0.24919068 -0.46048557 0.03574618 0.07458988

Input:

library(data.table)
set.seed(123)
df = data.table(x=rnorm(1000), y=rnorm(1000))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文