Paul
Paul

Reputation: 1117

Row sampling in R

I use the example data to ask the question.

seed(1) 
X <- data.frame(matrix(rnorm(200), nrow=20))

I wanted to select 10 random rows everytime without replacement and do a multiple regression. I tried

hi=X[sample(1:20,10),]
MR1<-lm(X10~., data=hi)
R1<-summary(MR1)$r.squared #extract the R squared

Is it possible to create 25 such datasets sampling 10 rows each time. In the end, I would like to store the sampled datasets and do a multiple regression and extract the r squared values from the 25 such models as well as well.

Upvotes: 3

Views: 102

Answers (1)

digEmAll
digEmAll

Reputation: 57210

You could use lapply:

set.seed(1) 
X <- data.frame(matrix(rnorm(200), nrow=20))

n <- 25
res <- lapply(1:n, 
              function(i) {
                samples <- sample(1:20,10)
                hi=X[samples,]
                MR1<-lm(X10~., data=X)
                R1<-summary(MR1)$r.squared
                return(list(Samples=samples,Hi=hi,MR1=MR1,R1=R1))
              })

Upvotes: 2

Related Questions