Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Data Analysis with R: Hypothesis Testing and Confidence Intervals, Exercises of Statistics

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

2017/2018

Uploaded on 02/15/2018

baseballguy1908
baseballguy1908 🇺🇸

1 document

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
.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; }
HW #7
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)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
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)
## Loading required package: mosaic
## Loading required package: dplyr
##
## Attaching package: 'dplyr'
##
## The following objects are masked from 'package:stats':
##
## filter, lag
##
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
##
## Loading required package: lattice
## Loading required package: ggplot2
## Loading required package: car
## Loading required package: mosaicData
##
## Attaching package: 'mosaic'
##
## The following object is masked from 'package:car':
##
## logit
##
## The following objects are masked from 'package:dplyr':
##
pf3
pf4
pf5

Partial preview of the text

Download Data Analysis with R: Hypothesis Testing and Confidence Intervals and more Exercises Statistics in PDF only on Docsity!

.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; }

HW

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)

speed dist

Min. : 4.0 Min. : 2.

1st Qu.:12.0 1st Qu.: 26.

Median :15.0 Median : 36.

Mean :15.4 Mean : 42.

3rd Qu.:19.0 3rd Qu.: 56.

Max. :25.0 Max. :120.

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)

Loading required package: mosaic

Loading required package: dplyr

Attaching package: 'dplyr'

The following objects are masked from 'package:stats':

filter, lag

The following objects are masked from 'package:base':

intersect, setdiff, setequal, union

Loading required package: lattice

Loading required package: ggplot

Loading required package: car

Loading required package: mosaicData

Attaching package: 'mosaic'

The following object is masked from 'package:car':

logit

The following objects are masked from 'package:dplyr':

count, do, tally

The following objects are masked from 'package:stats':

binom.test, cor, cov, D, fivenum, IQR, median,

prop.test,

quantile, sd, t.test, var

The following objects are masked from 'package:base':

max, mean, min, prod, range, sample, sum

require(Lock5Data)

Loading required package: 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)

group survived

1 Control Yes

2 Control Yes

3 Control Yes

4 Control Yes

5 Control Yes

6 Control Yes

prop((survived=="Yes")~group, data = penguin.data)

TRUE.Control TRUE.Experimental

0.62 0.

observed.difference<-diff(prop((survived=="Yes")~group, data = penguin.data)) prop((survived=="Yes") ~ shuffle(group), data=penguin.data)

TRUE.Control TRUE.Experimental

0.46 0.

-diff(prop((survived=="Yes") ~ shuffle(group), data=penguin.data))

TRUE.Experimental

-0.

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

TRUE

0.

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)

TRUE

1.

P.value

TRUE

1.

data("Moore") head(Moore)

partner.status conformity fcategory fscore

1 low 8 low 37

2 low 4 high 57

3 low 8 high 65

4 low 7 low 20

5 low 10 low 36

6 low 6 low 18

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

4.

P.value<-prop(~ (xbar.highstatus.minus.xbar.lowstatus>=observed.difference.con formity), data = random.differences.conformity) P.value

TRUE

0.

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.

  1. (^) 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.

  2. 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.

  3. The expected center of a bootstrap distribution is the sample mean. The expected center of a randomization distribution is where the null hypothesis lies.

  4. The commuters could have been randomly resampled and then replaced for each drawing.

data("BootAtlantaCorr") head(BootAtlantaCorr)

CorrTimeDist

1 0.

2 0.

3 0.

4 0.

5 0.

6 0.

lower<-quantile(~CorrTimeDist,data = BootAtlantaCorr, prob=0.005) upper<-quantile(~CorrTimeDist, data = BootAtlantaCorr, prob=0.995) CI<-c(lower,upper) CI

0.5% 99.5%

0.70600 0.

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.

2.5% 97.5%

0.729 0.

lower.90<-quantile(~CorrTimeDist,data = BootAtlantaCorr, prob=0.05) upper.90<-quantile(~CorrTimeDist, data = BootAtlantaCorr, prob=0.95)