简体   繁体   English

如何在R中使用for循环保存仿真数据

[英]How to save simulation data using for loop in R

I am simulating several data sets using for loop in R and saving the data sets in text files in a folder. 我正在R中使用for循环来模拟几个数据集,并将这些数据集保存在文本文件的文件夹中。 Since I need to analyze these data sets, I am importing these data from the folder to R and doing my analyses. 由于我需要分析这些数据集,因此将这些数据从文件夹导入到R中并进行分析。 I am wondering if there is any way to do both simulation and analysis by keeping them in R as data frames instead of saving and importing. 我想知道是否可以通过将它们作为数据帧保存在R中而不是保存和导入来进行仿真和分析。 Here is my code: 这是我的代码:

setwd("C:\\Users\\John\\Desktop\\datageneration")

kitem<-10
N<-100
disc<-rnorm(k,0,1)
diff=rnorm(k,0,1)

irtp<-function(t,a,b,pexp)
{
    pexp<-1/(1+exp(-b*(t-a)))
    pexp
}
for( iter in 1:20) 
{
    X<-mat.or.vec(N,kitem)
    P<-mat.or.vec(N,kitem)
    for(i in 1:N)
    {
        theta<-rnorm(N,0,1)
        assign(paste0("theta", iter), theta)
        filename1 <- paste (" theta",iter ,".txt ", sep ="")
        write.table( get(paste0("theta",iter)) , file = filename1 , row.names =FALSE ,col.names = FALSE )
        for(k in 1:kitem)
        {
            P[i,k]<-irtp(theta[i],diff[k],disc[k],pexp)
            X[i,k]<-ifelse(runif(1)<P[i,k],1,0)
            assign(paste0("X",iter), X)         # HERE'S THE PART THAT I NEED HELP
            filename2 <- paste ("X",iter ,".txt ", sep ="")
            write.table( get(paste0("X",iter)) , file = filename2 , row.names =FALSE ,col.names = FALSE )
        }
    }
}

All I want to do is just to use the generated data files (eg, theta1, theta2, theta3..., theta20) by calling their names(eg, theta1). 我只想通过调用生成的数据文件的名称(例如theta1)来使用生成的数据文件(例如theta1,theta2,theta3 ...,theta20)。 Since I am generating thousands of data sets, I would like to know if I can do it without using write.table then read.table functions. 由于我正在生成数千个数据集,所以我想知道是否可以在不使用write.table和read.table函数的情况下做到这一点。 I will be very appreciated if you can help me. 如果您能帮助我,我将不胜感激。

Edited to reflect the need for the X matrices: Create a list of 20 + 20 items with your simulation data, and name the members accordingly: 编辑以反映对X矩阵的需求:创建包含20 + 20个项目的列表,其中包含您的模拟数据,并相应地命名成员:

kitem<-10
N<-100
disc<-rnorm(kitem,0,1)  # not ( k, ... )
diff=rnorm(kitem,0,1)   # not ( k, ... )
pexp <- 1                   # ??? - not needed here

# the list that takes all the produced data
mySim <- as.list( NULL )

# function definition reduced to the necessary
irtp <- function( t, a, b ) {  1 / ( 1 + exp( -b * ( t -a ) ) ) }

for( iter in 1:20 )
{
  # create two matrices to be filled later
  X<-mat.or.vec(N,kitem)
  P<-mat.or.vec(N,kitem)

  # create and name the theta component
  theta = mySim[[ iter ]] <- rnorm( N, 0, 1 )
  names( mySim )[ iter ] <- paste ( "theta", iter, sep ="" )

  # fill and save the matrices
  for( i in 1:N )
  {
    for( k in 1:kitem )
    {
      P[i,k]<-irtp(theta[i],diff[k],disc[k] )  #  don"t need this: ,pexp)
      X[i,k]<-ifelse(runif(1)<P[i,k],1,0)
    }
  }
  mySim[[ 20 + iter ]] <- X
  names( mySim )[ 20 + iter ] <- paste ( "X", iter, sep ="" )
}

You can save the list altogether as an R object, if you want that. 如果需要,可以将列表完全保存为R对象。

Now you can adress each simulation be name: 现在您可以将每个模拟命名为:

head( mySim$theta3 )
[1]  0.96068066  0.01966067 -1.25682531 -0.15128916 -0.75950710 -1.22243883

You can add matrices, dataframes etc. to the list 您可以将矩阵,数据框等添加到列表中

mySim$tau1 <- c( "lists", "take", "everything" )

You can selectively save list members with the corresponding file name: 您可以选择使用相应的文件名来保存列表成员:

filename <- paste( names( mySim )[3], ".txt", sep = "" )
write.table( mySim$theta3, filename )

Is that what you had in mind? 那是你的想法吗?

目前尚不清楚您要做什么,但我认为这里需要replicate

ss <- replicate(20,replicate(N,rnorm(N,0,1)))

The objects in those Rda files will all have the same name, 'theta', so you will not be able to load them without having any earlier versions overwritten. 这些Rda文件中的对象都将具有相同的名称“ theta”,因此如果不覆盖任何早期版本,将无法加载它们。 If you want to save them without names, you can use saveRDS and readRDS giving them varying names. 如果要保存它们而不使用名称,则可以使用saveRDSreadRDS为它们提供不同的名称。 If you want to give them different names before then get save() -ed, then use assign and then save in the manner you are currently using. 如果要在给它们不同的名称之前获取save() ed,请使用assign ,然后以当前使用的方式保存。

All that said, I would be a lot easier to make a list of twenty such objects with varying names and then save them all at once and load them all at once. 这么说,我将列出二十个具有不同名称的此类对象的列表,然后一次将它们全部保存并立即加载它们会容易得多。

N=10
for ( iter in 1:3) 
{
      theta<-rnorm(N,0,1)
    assign(paste0("theta", iter), theta)
    filename1 <- paste (" theta",iter ,".txt ", sep ="")
    write.table( get(paste0("theta",iter)) , file = filename1 , row.names =FALSE ,col.names = FALSE )
}

> ls(patt="theta")
[1] "theta"  "theta1" "theta2" "theta3"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM