library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

One Proportion z Test p.244

prop.test(125,500,.22, alternative = "greater")
## 
##  1-sample proportions test with continuity correction
## 
## data:  125 out of 500, null probability 0.22
## X-squared = 2.4505, df = 1, p-value = 0.05874
## alternative hypothesis: true p is greater than 0.22
## 95 percent confidence interval:
##  0.218598 1.000000
## sample estimates:
##    p 
## 0.25

Two Proportion z Test p.286

prop.test(c(231,176),c(1000,1200),alternative = "greater")
## 
##  2-sample test for equality of proportions with continuity correction
## 
## data:  c(231, 176) out of c(1000, 1200)
## X-squared = 25.173, df = 1, p-value = 2.621e-07
## alternative hypothesis: greater
## 95 percent confidence interval:
##  0.05579805 1.00000000
## sample estimates:
##    prop 1    prop 2 
## 0.2310000 0.1466667

One Mean T-test

#expectancy_male<- expectancy %>% 
#  filter(sex == "Male", year == "2000")

#Expectancy_male<-Expectancy%>%

#filter(sex=="Male", year=="2000")

#head(Expectancy_male)

#Expectancy <- read.csv("https://krkozak.github.io/MAT160/Life_expectancy_Europe.csv")

#Expectancy_male<- Expectancy %>%
#filter(sex=="Male", year=="2000")

setwd("C:/Users/StarKid/Desktop/Data_Science/Data_101/week_5/IC10")
expectancy <- read.csv("life_expectancy.csv")

t.test(expectancy, mu=79.8, conf.level = 0.99, alternative = "less")
## 
##  One Sample t-test
## 
## data:  expectancy
## t = -7.7069, df = 52, p-value = 1.853e-10
## alternative hypothesis: true mean is less than 79.8
## 99 percent confidence interval:
##      -Inf 75.62445
## sample estimates:
## mean of x 
##  73.73585

Independent 2 sample-test p. 316

setwd("C:/Users/StarKid/Desktop/Data_Science/Data_101/week_5/IC10")
cholesterol <- read.csv("cholesterol.csv")


t.test(cholesterol$heartattack, cholesterol$healthy, conf.level = 0.99, alternative="greater")
## 
##  Welch Two Sample t-test
## 
## data:  cholesterol$heartattack and cholesterol$healthy
## t = 6.1452, df = 37.675, p-value = 1.86e-07
## alternative hypothesis: true difference in means is greater than 0
## 99 percent confidence interval:
##  36.7602     Inf
## sample estimates:
## mean of x mean of y 
##  253.9286  193.1333

Dependent paired 2 sample t-test

setwd("C:/Users/StarKid/Desktop/Data_Science/Data_101/week_5/IC10")
nzhelmet <- read.csv("nzhelmet.csv")

str(nzhelmet)
## 'data.frame':    18 obs. of  2 variables:
##  $ cardboard: int  146 151 163 152 151 151 149 166 149 155 ...
##  $ metal    : int  145 153 161 151 145 150 150 163 147 154 ...
t.test(nzhelmet$cardboard, nzhelmet$metal, paired = TRUE)
## 
##  Paired t-test
## 
## data:  nzhelmet$cardboard and nzhelmet$metal
## t = 3.1854, df = 17, p-value = 0.005415
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
##  0.5440163 2.6782060
## sample estimates:
## mean difference 
##        1.611111
qqnorm(nzhelmet$cardboard - nzhelmet$metal)