如何在for循环中循环浏览功能列表

时间:2019-08-21 14:50:15

标签: r list for-loop

我对使用R编程非常陌生,因此如果这个问题太基础,我深表歉意。我正在尝试使用由三个不同过程(即normal1,normal2和chi-square)创建的误差项研究OLS的属性。我将它们包括在“ fun_list”列表中。

我想遍历1,000个(iter)回归,每个回归的样本量为500(n)。我想将所有1,000 X 500个观测值以及回归结果(reg_results)保存在数据集(big_data)中。

在程序结束时,我希望对这三个过程分别进行1,000个回归(总共3,000个回归)。我已经在一个级别上为三个函数设置了嵌套循环,而在另一个(子)级别上设置了1,000次迭代。我无法让程序循环通过三个不同的功能。我不确定如何在此嵌入式循环中调出列表的每个元素。任何帮助将不胜感激!

 library(psych)
 library(arm)
 library(dplyr)
 library(fBasics)
 library(sjstats)

 #set sample size and number of iterations
 set.seed(12345)
 n <- 500
 iter <- 1000

 #setting empty vectors. Probably a better way to do this. :)
 bn <- rep(NA,iter)
 sen <- rep(NA,iter)


 #these are the three functions I want to use to generate en,
 #which is the error term below. I want one loop for each of the three.
 # I can get f1, f2 and f3 to work independently, but I can't get the list 
 #to work to cycle through all three. 

 f1 <- function (n)  {rnorm(n, 0, 2)} 
 f2 <- function (n)  {rnorm(n, 0, 10)} 
 f3 <- function (n)  {rchisq(n, 2)}
 fun_list <- list(f1, f2, f3)

 #following line starting point for saving all iterations in one big 
 #dataset

 datalist = list()

 #if I remove the following line (for (j ....)), I can get this to work by 
 #referencing each function independently (i.e., using 'en <- f1(n)').

 for (j in fun_list) {
    for (s in 1:iter) {

  #  en <- f1(n)
     en <- fun_list[[1]]
     x <- rnorm(n, 0, .5)
     yn <- .3*x + en

#this is the part that saves the data#
dat <- data.frame(yn, x, en)
dat$s <- s
datalist[[s]] <- dat

#### run model for normal data and save parameters###
lm1n <- lm(yn ~ x)
int.hatn <- coef (lm1n)[1]
b.hatn <- coef (lm1n)[2]
se.hatn <- se.coef (lm1n) [2]

##save them for each iteration
bn[s] = b.hatn
sen[s] = se.hatn
 }
 }

 reg_results<- tibble(bn, sen)

 big_data = do.call(rbind,datalist)

使用循环时,出现以下错误:

0.3 * x + en中的错误:二进制运算符的非数字参数

我认为这是因为我不完全了解如何调用列表中的三个功能。

1 个答案:

答案 0 :(得分:0)

这是一个完整的解决方案,其中包含注释中讨论的多个要点:

 library(psych)
 library(arm)
 library(dplyr)
 library(fBasics)
 library(sjstats)

 #set sample size and number of iterations
 set.seed(12345)
 n <- 500
 iter <- 1000

 #setting empty vectors. Probably a better way to do this. :)
 bn <- c()
 sen <- c()


 #these are the three functions I want to use to generate en,
 #which is the error term below. I want one loop for each of the three.
 # I can get f1, f2 and f3 to work independently, but I can't get the list 
 #to work to cycle through all three. 

 f1 <- function (n)  {rnorm(n, 0, 2)} 
 f2 <- function (n)  {rnorm(n, 0, 10)} 
 f3 <- function (n)  {rchisq(n, 2)}
 fun_list <- list(f1, f2, f3)

 #following line starting point for saving all iterations in one big 
 #dataset

 datalist = list()

 #if I remove the following line (for (j ....)), I can get this to work by 
 #referencing each function independently (i.e., using 'en <- f1(n)').

 for (j in c(1:length(fun_list))) {
   en <- fun_list[[j]]
   for (s in 1:iter) {
     x <- rnorm(n, 0, .5)
     random_part <- en(n)
     yn <- .3*x + random_part

     #this is the part that saves the data#
     dat <- data.frame(yn, x, random_part)
     dat$s <- s
     datalist[[s]] <- dat

     #### run model for normal data and save parameters###
     lm1n <- lm(yn ~ x)
     int.hatn <- coef(lm1n)[1]
     b.hatn <- coef(lm1n)[2]
     se.hatn <- se.coef(lm1n)[2]

     ##save them for each iteration
     bn = c(bn,b.hatn)
     sen = c(sen,se.hatn)
   }
 }

 reg_results<- tibble(bn, sen)

 big_data = do.call(rbind,datalist)