# Data: 30 randomly selected daily high temperatures from # summer days in New York City (May through September) summer.temps <- c(59,81,85,75,81,77,92,83,92,76,74,73,57,68,80,69, 83,78,87,88,93,71,73,76,81,82,73,64,80,79) mean(summer.temps) sd(summer.temps) # Test whether mean temperature is greater than 75: t.test(summer.temps, alternative="greater", mu=75) # Test whether mean temperature is different from 75: t.test(summer.temps, alternative="two.sided", mu=75) # 90% t confidence interval for population mean temperature: t.test(summer.temps, alternative="two.sided", conf.level=0.90) # Just the 90% confidence limits: t.test(summer.temps, conf.level=0.90)$conf.int ####### # # Calculating test statistic and P-value # when you just have the summary statistics: # y.bar <- 77.66667 s <- 8.872015 samp.size <- 30 # the hypothesized value of mu: mu.0 <- 75 # Calculating t* for a given y-bar, s, and n,: t.star <- (y.bar - mu.0)/(s/sqrt(samp.size)) # P-value for each possible type of alternative hypothesis: two.sided.P.value <- 2*(pt(abs(t.star), df = samp.size-1, lower.tail=FALSE)) print(paste("P-value for two-sided test is ", two.sided.P.value)) less.than.P.value <- pt(t.star, df = samp.size-1, lower.tail=TRUE) print(paste("P-value for less-than alternative is ", less.than.P.value)) greater.than.P.value <- pt(t.star, df = samp.size-1, lower.tail=FALSE) print(paste("P-value for greater-than alternative is ", greater.than.P.value)) ### 90% CI for mu: conf.level <- 0.90; alpha <- 1 - conf.level LCL <- y.bar - qt(1-alpha/2, df = samp.size-1)*(s/sqrt(samp.size)) UCL <- y.bar + qt(1-alpha/2, df = samp.size-1)*(s/sqrt(samp.size)) print(paste(conf.level*100, " percent CI for mu is ", LCL, UCL))