samplingR - mean(z) #display the mean of the...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Sheet1 Page 1 ###################### Sample from a population marked with 1's and 0's x=c(rep(0,5000),rep(1,5000)) #create a population with 5000 0's and 5000 1's. sample(x,10) #takes a random sample of size 10 from the population. mean(sample(x,10)) #gives the mean of a random sample of size 10 y=c(mean(sample(x,10))) #stores the mean of a random sample of size 10 y=c(y,mean(sample(x,10))) #adds the mean of a random sample of size 10 to the previous stored data #the following short program will add the mean of 1000 random samples of size ten to the previous store data for(i in 1:1000){ #this line tells the program to run 1000 times y=c(y,mean(sample(x,10))) #adds the mean of a random sample of size 10 to the previous stored data } hist(y) #makes a histogram of the stored means of the random samples. ######################## sample from a simulated Normal population z=rnorm(200,50,10) #create population of 200 values from the Normal distribution #with mean=50 and standard deviation=10
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: mean(z) #display the mean of the "population" zsamp=sample(z,16) #takes a sample of 16 of the values zsamp #display the sample mean(zsamp) #mean of the sample zmeans=NULL #intialize vector to store results from repeated sampling for(i in 1:100){ #run the sampling 100 times and save the results zmeans=c(zmeans,mean(sample(z,16))) } mean(zmeans) sd(zmeans) x11() hist(zmeans) # Notice that the sd for the sample means is about 2.5 = 10/4 # This illustrates the square root rule, that increasing the sample size # by a factor of n decreases the sd of the sample mean by a factor of sqrt(n) ####################### sample from a list based on id numbers # create some fake data testdata = data.frame(id=seq(12), vals=c(83,22,87,55,60,97,81,79,100,83,94,43)) testdata Sheet1 Page 2 attach(testdata) # sample 3 observations based on the id numbers idsamp=sample(id,3) idsamp #corresponding values vals[idsamp] # display sampled data testdata[idsamp,]...
View Full Document

Page1 / 2

samplingR - mean(z) #display the mean of the...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online