



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
R code and exercises for analyzing data, performing hypothesis tests, and calculating confidence intervals. Topics include markdown syntax, loading packages, data manipulation, and statistical analysis using r. Exercises on the 'penguin.data' and 'fertility.data' datasets.
Typology: Exercises
1 / 6
This page cannot be seen from the preview
Don't miss anything!
.main-container { max-width: 940px; margin-left: auto; margin-right: auto; } code { color: inherit; background-color: rgba(0, 0, 0, 0.04); } img { max-width:100%; height: auto; }
Sam Harris October 27, 2015 This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http:// rmarkdown.rstudio.com. When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this: summary(cars)
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot. require(mosaic)
prop.test,
require(Lock5Data)
set.seed(100210) penguin.data<-data.frame(group=rep(c("Control", "Experimental"), times=c(50,50)), survived=rep(c("Yes", "No", "Yes", "No"), times= c(31,19, 16, 34))) head(penguin.data)
prop((survived=="Yes")~group, data = penguin.data)
observed.difference<-diff(prop((survived=="Yes")~group, data = penguin.data)) prop((survived=="Yes") ~ shuffle(group), data=penguin.data)
-diff(prop((survived=="Yes") ~ shuffle(group), data=penguin.data))
random.differences<-do(10000)*-diff(prop((survived=="Yes") ~ shuffle(group), data=penguin.data)) names(random.differences)<-c("phat.control.minus.phat.metal") P.value<-prop((~phat.control.minus.phat.metal >= observed.difference), data = random.differences) P.value
histogram(~phat.control.minus.phat.metal, data = random.differences,groups = (phat.control.minus.phat.metal >= observed.difference),width = 1/50, cex = 10)
Exercise 1 set.seed(109210) fertility.data<-data.frame(group=rep(c("Fertile",
P.value.right <-prop(~(xbar.sleep.minus.xbar.caffeine
=observed.difference),data = random.differences) P.value.left <-prop(~(xbar.sleep.minus.xbar.caffeine <= - observed.difference),data = random.differences) (P.value <- P.value.left + P.value.right)
P.value
data("Moore") head(Moore)
group.means.conformity<-mean(conformity~partner.status, data = Moore) names(group.means.conformity)<- ("xbar.highstatus.minus.xbar.lowstatus") observed.difference.conformity<--diff(group.means.conformity) random.differences.conformity<-do(10000)*-diff(mean (conformity~shuffle(conformity), data= Moore)) names(random.differences.conformity)<- ("xbar.highstatus.minus.xbar.lowstatus") dotPlot(~xbar.highstatus.minus.xbar.lowstatus, data=random.differences.conformity, groups = xbar.highstatus.minus.xbar.lowstatus>=observed.difference.conf ormity | xbar.highstatus.minus.xbar.lowstatus<=observed.difference.conf ormity)
observed.difference.conformity
P.value<-prop(~ (xbar.highstatus.minus.xbar.lowstatus>=observed.difference.con formity), data = random.differences.conformity) P.value
My P-value for whether participants will change their mind based on status level of the partner was 19.06%, so it is insignificant and I can’t reject the null hypothesis. Book Problems: 2.a) A confidence interval. This will be easy to sample and bootstrap, then find a confidence interval. b) Hypothesis Test. This is a good case to measure a proportion difference using p-values. c) Neither. The population is pretty small (100), and it would be easy to collect all the data and then calculate the real percent. Also, the data is fairly
important and probably already stored somewhere. d) A confidence interval. Once we take a sample, it will be efficient to bootstrap and create a CI, and we can fairly certain whether the population parameter falls within the CI.
(^) The p-value means that there is a 2% chance that when the null hypothesis is true, the results of another sample will come back the same or more extreme. These results are significant which means that the increase in tax probably caused a decrease in soda consumption.
This p-value means that there is a 41% chance that when the null hypothesis is true, the results of another sample will come back the same or more extreme. This is not a significant result, and the null hypothesis is probably true. Taxes probably don’t effect soda consumption.
The expected center of a bootstrap distribution is the sample mean. The expected center of a randomization distribution is where the null hypothesis lies.
The commuters could have been randomly resampled and then replaced for each drawing.
data("BootAtlantaCorr") head(BootAtlantaCorr)
lower<-quantile(~CorrTimeDist,data = BootAtlantaCorr, prob=0.005) upper<-quantile(~CorrTimeDist, data = BootAtlantaCorr, prob=0.995) CI<-c(lower,upper) CI
The 99% confidence interval for the correlation in this setting is 0.706 to 0.875. This means that we are 99% confident that the correlation of distance and time of commutes for the population will fall within this interval. c) lower.95<-quantile(~CorrTimeDist,data = BootAtlantaCorr, prob=0.025) upper.95<-quantile(~CorrTimeDist, data = BootAtlantaCorr, prob=0.975) CI.95<-c(lower.95, upper.95) CI.
lower.90<-quantile(~CorrTimeDist,data = BootAtlantaCorr, prob=0.05) upper.90<-quantile(~CorrTimeDist, data = BootAtlantaCorr, prob=0.95)