Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Row sampling in R

Tags:

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.

like image 394
Paul Avatar asked Jun 29 '26 14:06

Paul


1 Answers

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))
              })
like image 134
digEmAll Avatar answered Jul 02 '26 04:07

digEmAll



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!