knitr::opts_chunk$set(echo = TRUE)

Question 1

MS 7.118 - pg 364

(a)

library(LaF)
# Function that samples the file
sample1 <- function(file, n) {
  lf <- laf_open(detect_dm_csv(file, sep = ",", header = TRUE, factor_fraction = -1))
  return(read_lines(lf, sample(1:nrow(lf), n)))
}
# Our sample
sample <- sample1("NZBIRDS.csv", 35)

(b)

mp <- c(-1,1)
mean(sample$Body.Mass) + mp*qt(1-.05,34)*sd(sample$Body.Mass)/sqrt(35)

(c)

The mean of the sampled birds' body masses will fall in the itnerval with 95% confidence

(d)

The interval is 95% confidence, which is why it is very likely to contain mu

(e)

mean(sample$Egg.Length) + mp*qt(1-.05,34)*sd(sample$Egg.Length)/sqrt(35)

The mean of the sampled birds' Egg Length will fall in the itnerval with 95% confidence

The interval is 95% confidence, which is why it is very likely to contain mu

(f)

n1 = 38 # total extinct
n2 = 78 # total not extinct

p1hat = 21 / n1  
q1hat = 1 - p1hat
p2hat = 7 / n2
q2hat = 1 - p2hat

mp = c(-1,1)
ci <- (p1hat - p2hat) + mp * qnorm(1-.05/2,0,1)*sqrt((p1hat*q1hat/n1) + (p2hat*q2hat/n2))
ci

Yes, theory is supported.

Question 2

MS 7.120 - pg 36

s1 <- rnorm(100, mean = 1312, sd = 422)
s2<- rnorm(47, mean = 1352, sd = 271)

t.test(s1,s2, conf.level = .95)

Question 3

MS 7.128 - pg 367

Question 4

MS 8.24 - pg 390

(a)

The null hypothesis is that $\mu = 2$ and the alternative hypothesis is that $\mu \neq2$

(b)

T: -1.02 and the P-value:0.322

(c)

The reject region is |T| < 2.1262

(d)

Since the pvalue is greater than alpha, we accept the null hypotehsis

(e)

Both testing methods can can test null hypothesis $\mu=0$ agains the alternative hypothesis $\mu \neq 0$

Question 5

MS 8.28 - pg 392

Question 6

MS 8.44 - pg 401

data <- read.csv("ORCHARD.csv")
foggy <- data[data$CONDITION == "FOG",]
cloudyClear <- data[data$CONDITION != "FOG",]

t.test(data$CONDITION == "FOG",data$CONDITION != "FOG", mu= 0, conf.level = .95)

We see the pvalue is > than alpha, thus we accept $H_0:\mu_1-\mu_2 = 0$

Question 7

MS 8.84 - pg 425

(a)

turbine <- read.csv("GASTURBINE.csv")

traditional <- turbine[turbine$ENGINE == "Traditional",]


aeroder <- turbine[turbine$ENGINE == "Aeroderiv",]

var.test(traditional$HEATRATE, aeroder$HEATRATE,conf.level = .95, var.equal=T)

We can see the pvalue is such that we do not have sufficient evidence to reject the null hypothesis.

(b)

advanced <-  turbine[turbine$ENGINE == "Advanced",]

var.test(advanced$HEATRATE, aeroder$HEATRATE,conf.level = .95, var.equal=T)

Question 8

MS 7.118 - pg 364

Question 9

MS 8.104 - pg 439

The NULL and alternative hypotheses are: $H_0: \mu_d = 0$ and $H_1: \mu_d \neq 0$

We will assume equal variances.

data<- read.csv("THRUPUT.csv")

t.test(data$HUMAN, data$AUTO, mu=0, paired = T)

We see that the pvalue is < than alpha (.05), therefore we must reject the null hypothesis.

Question 10

## sample function
set.seed(35) # This will give everyone the same sample
sam=round(rnorm(30,mean=20,sd=3),3)



########### bootstrap function ##################

myboot<-function(iter=10000,x,fun="mean",alpha=0.05,...){  #Notice where the ... is repeated in the code
n=length(x)   #sample size

y=sample(x,n*iter,replace=TRUE)
rs.mat=matrix(y,nr=n,nc=iter,byrow=TRUE)
xstat=apply(rs.mat,2,fun) # xstat is a vector and will have iter values in it 
ci=quantile(xstat,c(alpha/2,1-alpha/2))# Nice way to form a confidence interval
# A histogram follows
# The object para will contain the parameters used to make the histogram
para=hist(xstat,freq=FALSE,las=1,
main=paste("Histogram of Bootstrap sample statistics","\n","alpha=",alpha," iter=",iter,sep=""),
...,col = rainbow(5))

#mat will be a matrix that contains the data, this is done so that I can use apply()
mat=matrix(x,nr=length(x),nc=1,byrow=TRUE)

#pte is the point estimate
#This uses whatever fun is
pte=apply(mat,2,fun)
abline(v=pte,lwd=3,col="Black")# Vertical line
segments(ci[1],0,ci[2],0,lwd=4)      #Make the segment for the ci
text(ci[1],0,paste("(",round(ci[1],2),sep=""),col="Red",cex=3)
text(ci[2],0,paste(round(ci[2],2),")",sep=""),col="Red",cex=3)

# plot the point estimate 1/2 way up the density
text(pte,max(para$density)/2,round(pte,2),cex=3)

return(list(ci=ci,fun=fun,x=x, t=t))# Some output to use if necessary
}

myboot(x=sam)


agracy2246/MATH4753grac0009 documentation built on April 26, 2020, 9:39 a.m.