具有NA的矩阵的滚动回归

时间:2019-07-29 02:33:23

标签: r function regression na rolling-computation

我使用24个月的数据对Mat1中的行相对于Mat2中的行执行滚动回归。我对矩阵中的所有列重复此过程。例如,对于给定的行(例如row1):回归1:lm(mat1 [1,1:24]〜mat2 [1,1:24]),回归2:lm(mat1 [1,2:25] 〜mat2 [1,2:25])...在所有列(最多m个)和行(n)上重复此过程。然后,我将截距,β系数,t值和R平方值存储在单独的矩阵中。当Mat1和Mat2没有NA时,我的代码可以工作,但是Mat1和Mat2没有NA时,情况并非如此。

设置

Mat1 <- matrix(nrow= 483,  ncol=30,  data= rnorm(14490, 0,1))
Mat1[42:483,1:24] <- NA
Mat1[43:483,25:30] <-NA

Mat2 <- matrix(nrow = 483, ncol=30, data= rnorm(14490, 0,1))
Mat2[42:483,1:24] <- NA
Mat2[43:483,25:30] <-NA

cohort_alpha_function <- function(mat1, mat2, output) {
  # output can take values "alpha" or "beta"
  alpha_matrix <- matrix(nrow = nrow(mat1), ncol =(30-24))
  beta1_matrix <- matrix(nrow = nrow(mat1), ncol =(30-24))
  t_value_matrix_ca <- matrix(nrow = nrow(mat1), ncol =(30-24))
  p_value_matrix_ca <- matrix(nrow = nrow(mat1), ncol =(30-24))
  R_squared_ca <- matrix(nrow = nrow(mat1), ncol =(30-24))
  for (i in 1:nrow(mat1)){
    for(j in 1:length(seq_len(30-24))) {
      a <- seq_len((30-24))
      b <- seq(25,30,1)
      c <- seq_len((30-24))
      d <- seq(25, 30,1)
      alpha_matrix[i,j] = coefficients(lm(mat1[i, a[j]:b[j]] ~ mat2[i, c[j]:d[j]]))[[1]]
      beta1_matrix[i,j] = coefficients(lm(mat1[i, a[j]:b[j]] ~ mat2[i, c[j]:d[j]]))[[2]]
      t_value_matrix_ca[i,j]= summary(lm(mat1[i, a[j]:b[j]] ~ mat2[i, c[j]:d[j]]))$coef[, "t value"][2]
      p_value_matrix_ca[i,j]=summary(lm(mat1[i, a[j]:b[j]] ~ mat2[i, c[j]:d[j]]))$coef[, "Pr(>|t|)"][2]
      R_squared_ca[i,j] = summary(lm(mat1[i, a[j]:b[j]] ~ mat2[i, c[j]:d[j]]))$r.squared
    }
  }
  if(output == "alpha") {return(alpha_matrix)}
  if(output == "beta") {return(beta1_matrix)}
  if(output == "t_value") {return(t_value_matrix_ca)}
  if(output == "p_value") {return(p_value_matrix_ca)}
  if(output == "R_squared") {return(R_squared_ca)}
}




cohort_alpha_matrix <- cohort_alpha_function(mat1 = as.matrix(Mat1), mat2 = as.matrix(Mat2), output = "alpha")
cohort_alpha_beta1_matrix <-  cohort_alpha_function(mat1 = as.matrix(Mat1), mat2 = as.matrix(Mat2), output = "beta")
cohort_alpha_t_values <- cohort_alpha_function(mat1 = as.matrix(Mat1), mat2= as.matrix(Mat2), output = "t_value")
cohort_alpha_R_squared <- cohort_alpha_function(mat1 = as.matrix(Mat1), mat2 = as.matrix(Mat2), output = "R_squared")

如果我在Mat1 = mat1 <-matrix(nrow = 483,ncol = 30,data = rnorm(14490,0,1))和mat2 = mat2 <-matrix(nrow = 483,ncol = 30,data = rnorm(14490,0,1)),它正常工作,我得到正确的输出。但是,当我将mat1和mat2分别定义为:

Mat1 <- matrix(nrow= 483,  ncol=30,  data= rnorm(14490, 0,1))
Mat1[42:483,1:24] <- NA
Mat1[43:483,25:30] <-NA

Mat2 <- matrix(nrow = 483, ncol=30, data= rnorm(14490, 0,1))
Mat2[42:483,1:24] <- NA
Mat2[43:483,25:30] <-NA

不是。

当mat1 = Mat1 <-matrix(nrow = 483,ncol = 30,data = rnorm(14490,0,1)),并且mat2 = Mat2 <-matrix(nrow = 483, ncol = 30,data = rnorm(14490,0,1)),定义cohort_alpha_function,然后运行代码:

cohort_alpha_matrix <- cohort_alpha_function(mat1 = as.matrix(Mat1), mat2 = as.matrix(Mat2), output = "alpha")
cohort_alpha_beta1_matrix <-  cohort_alpha_function(mat1 = as.matrix(Mat1), mat2 = as.matrix(Mat2), output = "beta")
cohort_alpha_t_values <- cohort_alpha_function(mat1 = as.matrix(Mat1), mat2= as.matrix(Mat2), output = "t_value")
cohort_alpha_R_squared <- cohort_alpha_function(mat1 = as.matrix(Mat1), mat2 = as.matrix(Mat2), output = "R_squared")

当在Mat1和Mat2的定义中存在NA,但在输出中具有NA的地方(无法定义回归)时,我希望得到相同的结果。 Na.omit和na.exclude对我不起作用。

0 个答案:

没有答案