0

I'm trying to figure out how to repeat the same code 30 times without typing each one at a time... any help will be much appreciated.

SRS_1 <- sample(1:nrow(MyData_points), size=.10*nrow(MyData_points))

data_sample_1 <- MyData_points[SRS_1,]

fpc.srs <- rep(6399875, 639987)

design_SRS_1 <- svydesign(id=~1, strata=NULL, data=data_sample_1, fpc=fpc.srs)

ONStotal_SRS1 <- svytotal(~data_sample_1$V4, design=design_SRS_1)

ONSmean_SRS1 <- svymean(~data_sample_1$V4, design=design_SRS_1)

CI_SRS_1 <- confint(svytotal(~data_sample_1$V4, design=design_SRS_1))

The first code calculates a Simple Random Sampling with a probability of .10 from the data. The second gets the sample from the data. Third, calculates the fpc, which is the 10% of the total data points. Now, in order to estimate the population I need to do a design of the sample without replacement including the fpc. Then, for the last three codes, I calculate a population estimate, mean and confidence interval based on that sample. What changes is that I must repeat 30 different Simple Random Samplings from the data. Therefore, the resulting estimation, mean and confidence intervals will be obtained from 30 different samples. They might be close but not equal

How can I make this code better so I can run it 30 times each and be able to print a table with (ONStotal_SRS1, ONSmean_SRS1,CI_SRS_1)?

Paola
  • 21
  • 3
  • I wish whoever downvoted would explain why they did, it might be helpful. Anyhow, what is changing over the 30 times? For each of those 30, are you wanting a row of a table? – Brian Stamper May 02 '18 at 14:50
  • Thank you for the comment. The first code calculates a Simple Random Sampling with a probability of .10 from the data. The second gets the sample from the data. Third, calculates the fpc, which is the 10% of the total data points. Now, in order to estimate the population I need to do a design of the sample without replacement including the fpc. Then, for the last three codes, I calculate a population estimate, mean and confidence interval based on that sample. – Paola May 02 '18 at 14:54
  • Please, have a look here: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Terru_theTerror May 02 '18 at 15:01
  • What changes is that I must repeat 30 different Simple Random Samplings from the data. Therefore, the resulting estimation, mean and confidence intervals will be obtained from 30 different samples. They might be close but not equal. – Paola May 02 '18 at 15:04
  • Please move your comments into the question itself by clicking on the edit button. They are an important part of your question, and not everyone will read the comments. (and I'm upvoting to bring you back to 0!) – Mozahler May 02 '18 at 15:20

1 Answers1

0

Usually I would use either rbindlist from the data.table package or bind_rows from dplyr in combination with an lapply to build the table a row at a time and then bind the rows together. Here is an example using bind_rows with the mtcars data set:

library(dplyr)

combined_data <- bind_rows(lapply(1:30, function(...) {
  # Take a sample
  SRS_1 <- sample(1:nrow(mtcars), size = .10 * nrow(mtcars))
  data_sample_1 <- mtcars[SRS_1, ]

  # Compute some things from the sample
  m_disp <- mean(data_sample_1$disp)
  m_hp <- mean(data_sample_1$hp)

  # Make a one row data.frame that will be returned by the function
  data.frame(m_disp, m_hp)
}))

Which gives this data.frame:

> str(combined_data)
'data.frame':   30 obs. of  2 variables:
$ m_disp: num  235 272 410 115 249 ...
$ m_hp  : num  147 159 195 113 154 ...
Brian Stamper
  • 2,143
  • 1
  • 18
  • 41
  • By the way, the base R way of doing this is `do.call("rbind", lapply(1:30, function(...) {`, etc., but with larger data sets `dplyr` or `data.table` will be more efficient. – Brian Stamper May 02 '18 at 15:13