How to do this question in RStudio with explanation. Using the sampling distribu
ID: 3177459 • Letter: H
Question
How to do this question in RStudio with explanation. Using the sampling distribution of sample mean to estimate the mean of the variables "xv" and "ys" in the data "scatter1.txt" from the R book.
(a) For the simulation do the sampling 2000 times with sample size n=20 and n=50.
(b) Use the sampling distribution(with n=20) that you constructed in (a) to compute an approximation of the probability of the sample mean falling within .5 of the mean of each variable "xv" and "ys".
(c) Do the same question for (b) with n=50.
Explanation / Answer
scatter1 <- read.table("C:\Users\Aswin\Downloads\scatter1.txt", sep = " ", header = T) #read the file
m <- 2000 # Number of samples
n1 <- 20 # size of 1st sample
n2 <- 50 # size of 2nd sample
scatter1XV <- as.numeric(as.matrix((scatter1$xv))) #convert to a single column matrix
scatter1YS <- as.numeric(as.matrix((scatter1$ys)))
#For sample size 20
scatter1XV.Means20 <- replicate(m, mean(sample(scatter1XV,n1,replace=TRUE))) #replicate the sampling 2000 times
scatter1YS.Means20 <- replicate(m, mean(sample(scatter1YS,n1,replace=TRUE)))
xv.Mean1 <- mean(scatter1XV.Means20) # To find the expected value of the means
ys.Mean1 <- mean(scatter1YS.Means20)
xv.Mean1
ys.Mean1
#For sample size 50
scatter1XV.Means50 <- replicate(m, mean(sample(scatter1XV,n2,replace=TRUE)))
scatter1YS.Means50 <- replicate(m, mean(sample(scatter1YS,n2,replace=TRUE)))
xv.Mean2 <- mean(scatter1XV.Means50)
ys.Mean2 <- mean(scatter1YS.Means50)
xv.Mean2
ys.Mean2
#For approximating probability of mean of sample means falling within 0.5 of true mean(sample size 20)
pnorm(xv.Mean1 + 0.5, mean = xv.Mean1, sd = sd(scatter1XV.Means20)/sqrt(n1)) -
pnorm(xv.Mean1 - 0.5, mean = xv.Mean1, sd = sd(scatter1XV.Means20)/sqrt(n1)) #XV variable
pnorm(ys.Mean1 + 0.5, mean = ys.Mean1, sd = sd(scatter1YS.Means20)/sqrt(n1)) -
pnorm(ys.Mean1 - 0.5, mean = ys.Mean1, sd = sd(scatter1YS.Means20)/sqrt(n1)) #YS variable
#For approximating probability of mean of sample means falling within 0.5 of true mean(sample size 50)
pnorm(xv.Mean2 + 0.5, mean = xv.Mean2, sd = sd(scatter1XV.Means50)/sqrt(n2)) -
pnorm(xv.Mean2 - 0.5, mean = xv.Mean2, sd = sd(scatter1XV.Means50)/sqrt(n2)) #XV variable
pnorm(ys.Mean2 + 0.5, mean = ys.Mean2, sd = sd(scatter1YS.Means50)/sqrt(n2)) -
pnorm(ys.Mean2 - 0.5, mean = ys.Mean2, sd = sd(scatter1YS.Means50)/sqrt(n2)) #YS variable
The sampling distribution follows a normal distribution as long as the number of elements are greater than 30, hence the usage of function pnorm
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.