我试图通过生成起始值,然后使用引用其他列的以下“时间点”方程来模拟宽数据框中的面板数据。我想让n = t = 10500的总观测值为n = 500,t = 21。
我已经从多元正态分布模拟了一些初步观察结果(即t = 0),然后生成了一个宽数据框,其中包含用于接下来的20个连续``时间点''(即t> 0)的占位符列。到目前为止,我的数据看起来像这样(仅显示前15列):
id xt0 yt0 wt0 zt0 xt1 yt1 xt2 yt2 xt3 yt3 xt4 yt4 xt5 yt5
1 0.1251041 -0.5202968 0.7444884 -0.1655747 -0.4636554 NA NA NA NA NA NA NA NA NA NA
22 -1.4346734 0.2313276 4.4026209 0.6278826 -0.2208187 NA NA NA NA NA NA NA NA NA NA
43 3.5234120 1.6617287 1.8510357 0.3680791 1.3616461 NA NA NA NA NA NA NA NA NA NA
64 1.9857540 1.5927217 -1.7127041 -0.1823246 0.6032245 NA NA NA NA NA NA NA NA NA NA
85 2.0633524 1.8347750 1.5244358 0.5539688 0.5201867 NA NA NA NA NA NA NA NA NA NA
106 2.1106941 -1.1516203 -0.1515743 -0.5320694 0.1345948 NA NA NA NA NA NA NA NA NA NA
我现在想找到一种有效的方法来执行此操作,因为在20个时间点和两个变量的情况下,像这样写出每个等式会很麻烦:
param <- c( 0.2, 0.5, 1.0)
df$xt1 <- param[ 1]*df$xt0 + param[ 2]*df$yt0 + param[ 3]*df$wt0 + rnorm( 500, 0, 1)
df$yt1 <- param[ 1]*df$yt0 + param[ 2]*df$xt0 + param[ 3]*df$zt0 + rnorm( 500, 0, 1)
df$xt2 <- param[ 1]*df$xt1 + param[ 2]*df$yt1 + param[ 3]*df$wt0 + rnorm( 500, 0, 1)
...
df$yt20 <- param[ 1]*df$yt19 + param[ 2]*df$xt19 + param[ 3]*df$zt0 + rnorm( 500, 0, 1)
请注意,模式为:系数1 *滞后变量+系数2 *交叉滞后变量+系数3 *常数+随机噪声。
这意味着对于每个方程,自回归列和交叉滞后列都会发生变化,但常数始终来自同一列。这使代码变得更复杂,我无法确定。
到目前为止,我已经尝试过:
for( i in df[ , 5:ncol( df)]) {
if( lapply( ncol( df[ , i], '%%', 2) == 0)) {
df[ , i] <- param[ 1]*df[ , ( i - 4)] + param[ 2]*df[ , ( i - 3)] + param[ 3]*df$wt0 + rnorm( 500, 0, 1)
} else {
df[ , i] <- param[ 1]*df[ , ( i - 4)] + param[ 2]*df[ , ( i - 3)] + param[ 3]*df$zt0 + rnorm( 500, 0, 1)
}
}
我要使用此代码执行的操作是:
我遇到错误
Error in match.fun(FUN) : argument "FUN" is missing, with no default
但是这些代码只是我在互联网上其他地方找到的片段中拼凑而成的。我确信它有很多问题。因此,我将不胜感激!谢谢!
编辑:这是数据帧的前6行和15列的dput输出。
structure(list(id = c(-0.409761767417203, -2.16821996677264,
3.26224532017912, 1.99293159129652, 0.18786842037283, -0.403158586535083
), xt0 = c(-1.08325121689622, -0.257402826652493, 4.51801329740704,
2.00766925507723, 2.05915331375632, 0.893125999249676), yt0 = c(0.744488400264831,
4.4026209400443, 1.85103568019496, -1.71270414984045, 1.52443576496701,
-0.151574338169848), wt0 = c(-0.165574724549133, 0.627882638545303,
0.368079129633285, -0.182324586908647, 0.553968819325837, -0.532069428215114
), zt0 = c(-0.463655350080687, -0.220818663029958, 1.36164608113302,
0.603224522813769, 0.520186714928409, 0.134594809514275), xt1 = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), yt1 = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), xt2 = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), yt2 = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), xt3 = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), yt3 = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), xt4 = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), yt4 = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), xt5 = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), yt5 = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_)), row.names = c(1L,
22L, 43L, 64L, 85L, 106L), class = "data.frame")