'错误替换有100行,数据有0'跟随循环模拟

时间:2018-02-15 20:47:29

标签: r

我需要运行1999年的模拟,其中我创建了两个ARMA(1,0)模型,即 a(t)= a(t-1)+ e(t)和b(t)= b(t-1)+ v(t)因此我可以运行回归模型 a(t)= c * b(t)+ u(t)并存储系数c,其标准误差及其t统计量。我需要在最后获得1999年的统计数据。

with testing.postgresql.Postgresql(port=7654) as psql:
    # do something here

我收到以下错误:

num_reps <- 1999 num_obs <- 200 phi_1_1 <- 0.99 theta_1_1 <- 0 sigma_2 <- 1 arma_stats <- data.frame(time=1:num_obs, t_stat= numeric(num_obs)) time_series <- data.frame(time = 1:num_obs, arma_1_0y= numeric(num_obs), arma_1_0x = numeric(num_obs)) for (rep_num in 1:num_reps) { time_series[, 'arma_1_0y'] <- arima.sim(n = num_obs, list(ar = c(phi_1_1), ma = c(theta_1_1)),sd = sqrt(sigma_2)) time_series[, 'arma_1_0x']<- arima.sim(n = num_obs, list(ar = c(phi_1_1), ma = c(theta_1_1)),sd = sqrt(sigma_2)) fmla_string <- 'arma_1_0y ~ 0 + arma_1_0x' fmla <- as.formula(fmla_string) arma_1_0 <- lm(data = time_series, formula = fmla) coeff <- arma_1_0$coef std_err <- sqrt(diag(arma_1_0$var.coef)) tstat <- coeff/std_err arma_stats[rep_num, 't_stat'] <- tstat } 中的错误([<-.data.frame ,,&#34; arma_1_0y&#34;,值= c(7.57073355990616,:   替换有100行,数据有0

我是R的初学者。我做错了什么?非常感谢你!

1 个答案:

答案 0 :(得分:0)

错误派生在标准错误 std_err 中,因为它返回一个空矩阵。为了从lm调用中检索参数估计(即,。coeff,t-stats,std.err,p-value),请考虑使用coeff()对象中的summary(model)

res <- summary(arma_1_0)  # FIRST ITERATION
coef(res)

#            Estimate Std. Error  t value     Pr(>|t|)
# arma_1_0x 0.5024706 0.05034849 9.979854 2.824744e-19

此外,由于您不动态构建公式,因此无需转换字符串公式,因为您可以直接在lm中包含静态公式对象。因此,您可以相应地进行调整:

for (rep_num in seq(num_reps)) {

  time_series[, 'arma_1_0y'] <- arima.sim(n = num_obs, list(ar = c(phi_1_1), 
                                                            ma = c(theta_1_1)),
                                                            sd = sqrt(sigma_2))

  time_series[, 'arma_1_0x'] <- arima.sim(n = num_obs, list(ar = c(phi_1_1), 
                                                           ma = c(theta_1_1)),
                                                           sd = sqrt(sigma_2))

  arma_1_0 <- lm(arma_1_0y ~ 0 + arma_1_0x, data = time_series)

  res <- summary(arma_1_0)      
  tstat <- coef(res)[, "t value"]
  arma_stats[rep_num, 't_stat'] <- tstat      

}