Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R中的OLS回归_R_Regression_Combinations - Fatal编程技术网

R中的OLS回归

R中的OLS回归,r,regression,combinations,R,Regression,Combinations,我真的在用R解决下面的问题集 . 我想模拟一个数据集,有一个因变量和20个自变量,通常是I.I.d.,每个变量应该有100个观察值。我设法做到了这一点 现在,我正在努力解决的问题是: 我的计划是使用自己的编码回归函数对最多5个回归器的所有可能组合进行自动回归,该函数模拟summarylm的输出,使用向量y和矩阵或向量x作为输入,因此My.lmy,x。然后将结果引入合适的数据结构中 我会感谢每一个提示 我怀疑你想做的事情是否正确,但事情就这样发生了。 我将创建一个数据集,因为您还没有发布数据集 m

我真的在用R解决下面的问题集 . 我想模拟一个数据集,有一个因变量和20个自变量,通常是I.I.d.,每个变量应该有100个观察值。我设法做到了这一点

现在,我正在努力解决的问题是: 我的计划是使用自己的编码回归函数对最多5个回归器的所有可能组合进行自动回归,该函数模拟summarylm的输出,使用向量y和矩阵或向量x作为输入,因此My.lmy,x。然后将结果引入合适的数据结构中


我会感谢每一个提示

我怀疑你想做的事情是否正确,但事情就这样发生了。 我将创建一个数据集,因为您还没有发布数据集

my.lm <- function(x, y, n = 5){
  f <- function(inx){
    inx_cols <- Combn[inx, ]
    inx_cols <- inx_cols[inx_cols != 0]
    X <- as.data.frame(x[, inx_cols])
    names(X) <- paste0("X", inx_cols)
    X <- cbind(X, y)
    name_y <- names(X)[length(names(X))]
    fmla <- as.formula(paste(name_y, ".", sep = "~"))
    tryCatch(lm(fmla, data = X), error = function(e) e)
  }

  nc_x <- ncol(x)
  nr <- sum(choose(nc_x, seq_len(n)))
  Combn <- matrix(0, nrow = nr, ncol = n)
  first <- 1
  for(i in seq_len(n)){
    last <- first + choose(nc_x, i) - 1
    Combn[first:last, seq_len(i)] <- t(combn(nc_x, i))
    first <- last + 1
  }

  apply(Combn, 1, f)
}

set.seed(6876)

regr <- replicate(20, rnorm(100))
coefs <- sample(-5:5, 20, TRUE)
resp <- regr %*% coefs + rnorm(100)

lm_list <- my.lm(regr, resp)
length(lm_list)
#[1] 21699

你见过20乘5有多少种组合吗?选择20,5返回[1]15504。加上选择20,4,选择20,3,选择20,2,选择20,1。不是我的反对票,但你的程序有一点?是的,我已经谈到了有多少种组合是可能的。我正在努力以数据结构的形式获取每个组合。这是我尝试过的方法。table@falkialki,您应该添加您尝试过的内容,即您在问题的评论中写下的内容
err_list <- lapply(lm_list, function(x){
  if(inherits(x, "error")) x else NULL
})
err_list <- err_list[!sapply(err_list, is.null)]
length(err_list)
#[1] 0
good_list <- lapply(lm_list, function(x){
  if(inherits(x, "lm")) x else NULL
})
good_list <- good_list[!sapply(good_list, is.null)]

smry_list <- lapply(good_list, summary)
smry_list[[1]]
#
#Call:
#  lm(formula = fmla, data = X)

#Residuals:
#    Min      1Q  Median      3Q     Max 
#-34.654  -9.487  -1.985   9.486  50.213 

#Coefficients:
#                Estimate Std. Error t value Pr(>|t|)    
#(Intercept)       0.6449     1.5237   0.423    0.673    
#X1               -7.3969     1.5074  -4.907 3.68e-06 ***
#  ---
#  Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#
#Residual standard error: 15.02 on 98 degrees of freedom
#Multiple R-squared:  0.1972,   Adjusted R-squared:  0.189 
#F-statistic: 24.08 on 1 and 98 DF,  p-value: 3.684e-06