[R] subsamples and regressions for 100 times

Angela Smith angela.smith2071 at hotmail.com
Tue Feb 17 13:40:54 CET 2015



Hi R user,
I'm new to R so
my problem is probably pretty simple but I'm stuck:



my data is consist of 2 variables: co2, temp and one
treatment (l_group). The sample size is different among the treatments. so
that, I wanted to make equal sample size among three groups (A,B and C) of the
treatment.

For this one, I used subsamples technique. Using
subsample, each time the data are different among the three groups of the
treatment.

so that I want to run regression (co2~temp) for a 100
subsamples for each group of treatment (100 times subsample). 

it means that I will have 100 regression equations.  Later, I want to compare the slope of the
regression among the three groups. is there simple way to make a loop so that I
can compare it?

Thanks in advance!



Angela

================
Here is the example:

dat<-structure(list(co2 = c(0.15, 0.148, 0.125, 0.145, 0.138, 0.23, 
0.26, 0.35, 0.41, 0.45, 0.39, 0.42, 0.4, 0.43, 0.26, 0.3, 0.34, 
0.141, 0.145, 0.153, 0.151, 0.128, 0.23, 0.26), temp = c(0.0119, 
0.0122, 0.0089, 0.0115, 0.0101, 0.055, 0.097, 0.22, 0.339, 0.397, 
0.257, 0.434, 0.318, 0.395, 0.087, 0.13, 0.154, 0.0107, 0.0112, 
0.0119, 0.012, 0.0092, 0.055, 0.089), L_group = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("A", "B", "C"), class = "factor")), .Names = c("co2", 
"temp", "L_group"), class = "data.frame", row.names = c(NA, -24L
))

head(dat)
library(sampling)

# strata.sampling -----
strata.sampling <- function(data, group,size, method = NULL) {
 require(sampling)
  if (is.null(method)) method <- "srswor" 
  temp <- data[order(data[[group]]), ]
  ifelse(length(size)> 1,
         size <- size,
         ifelse(size < 1,
                size <- round(table(temp[group]) * size),
                size <- rep(size, times=length(table(temp[group])))))
  strat = strata(temp, stratanames = names(temp[group]),
                 size = size, method = method)
  getdata(temp, strat)
}

#--------------------------------------------------
sub_dat <- strata.sampling(dat, 'L_group', 4)# 
Lmodel_subdata1<-lm(co2~temp, data=subdat)
Lmodel_subdata1#coef

sub_dat2 <- strata.sampling(dat, 'L_group', 4)# 
Lmodel_subdata2<-lm(co2~temp, data=subdat2)
Lmodel_subdata2#coef

and so on.....[for 100 times)

Table<-rbind(Lmodel_subdata1$coef, Lmodel_subdata1$coef, ....)


 		 	   		  


More information about the R-help mailing list