##In a study of larval development in the tufted apple budmoth, an entomologist measured the head widths of 50 larvae. All 50 had been reared under identical conditions. The mean head width was 1.20 mm and the standard deviation was 0.14 mm. ##(1 pt) Construct a 95% confidence interval for the population mean (you can do it by hand, by R, or by calculator).

y<-1.2
s<-.14
n<-50
df<-n-1
SE<-.14/sqrt(n)

CV<-qt(.975,df)

upper_vector<-y+CV*SE
lower_vector<-y-CV*SE
CI<-c(lower_vector,upper_vector);CI
## [1] 1.160212 1.239788

##(2 pts) Interpret this interval in the context of the problem. We are 95% confident that the true population mean larval head width for tufted apple budmoths is between 1.160 mm and 1.240 mm. ## (1 pt) How would decreasing the confidence level change the width of the interval? By decreasing the confidence level the width of the interval would be narrowed. This is exemplified in below by the difference in the critical value for creating a 95% CI versus a 90% CI. Since the CV is multiplied by SE to create CI, a less “confident” CI is narrower. This makes sense intuitively in that to be more sure that we have captured a population mean in a CI, we would widen the CI and vice versa.

CV95<-qt(.975,df);CV95
## [1] 2.009575
CV90<-qt(.95,df);CV90
## [1] 1.676551

##(1 pt) How would increasing the sample size change the width of the interval? Increasing the sample size would narrow the width of the interval exemplified below. This occurs because the SE will be decreased which narrows the CI. This is intuitive in that increasing n of a sample (up to less than 10% population N) decreases SE by inceasing the sample’s representativeness of the population.

s<-.14
n<-50
SE<-.14/sqrt(n)
SE
## [1] 0.01979899
n<-1000
SE<-.14/sqrt(n)
SE
## [1] 0.004427189

##(2 pts) A plant physiologist is planning to measure the stem lengths of soybean plants after 2 weeks of growth when using a new fertilizer. Previous experiments suggest that the standard deviation of a stem length is around 1.2 cm. Using this as a guess of , determine how many soybean plants the researcher should have if she wants the standard error of the group mean to be no more than 0.2 cm. n= 36 or more soybean plants

s<-1.2
SE<-.2
n<-(s^2)/(SE^2);n
## [1] 36

##(1 pt) Construct a 99% confidence interval for the difference in population means, assuming that the two populations are normally distributed. Ran tsum.tet to get df (Chunk 1) then constructed 99% CI (chunk2).

library(BSDA)
## Loading required package: lattice
## 
## Attaching package: 'BSDA'
## The following object is masked from 'package:datasets':
## 
##     Orange
tsum.test(25,10,10,23,8,10)
## 
##  Welch Modified Two-Sample t-Test
## 
## data:  Summarized x and y
## t = 0.49386, df = 17.173, p-value = 0.6277
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -6.537567 10.537567
## sample estimates:
## mean of x mean of y 
##        25        23
df<-17.173
CV<-qt(.995, df)
y1<-25
y2<-23
s1<-10
s2<-8
n1<-10
n2<-10

y_dif<-y1-y2
SE_Y1Y2<- sqrt(((s1^2)/n1)+((s2^2)/n2))

upper_vector<-y_dif+CV* SE_Y1Y2
lower_vector<-y_dif-CV* SE_Y1Y2

CI<-c(lower_vector, upper_vector); CI
## [1] -9.722335 13.722335

##(2 pts) Interpret the confidence interval in the context of the problem.
We are 99% confident that an antibiotic used in rats can speed up clotting by as much as 9.722 seconds and slow down clotting by as much as 13.722 seconds. This confidence interval also contains 0 so it is unlikely the antibiotic and control samples are statistically different.

##(2 pts) Calculate a 95% confidence interval for the difference in mean amount of bacteria between the two treatments, and interpret in context of the problem. We are 95% confident that triclosan’s affect on E. coli colony muliplication over 24 hours can reduce colonies over a 24 hour period by as much as 32.029 colonies or increase it by as much as 13.387 colonies over control group. Because CI contains zero it is statistically unlikely that there is a differnece in the population between soaps that do and do not use Triclosan on killing bacteria (E. coli)

control<-c(30,36,66,21,63,38,35,45)
soap<-c(76,27,16,30,26,46,6)
 
yc<-mean(control)
sc<-sd(control)
nc<-length(control)

ys<-mean(soap)
ss<-sd(soap)
ns<-length(soap)

library(BSDA)
tsum.test(ys,ss,ns,yc,sc,nc)
## 
##  Welch Modified Two-Sample t-Test
## 
## data:  Summarized x and y
## t = -0.90954, df = 10.43, p-value = 0.3836
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -32.02964  13.38678
## sample estimates:
## mean of x mean of y 
##  32.42857  41.75000