exam_score <- c(85, 88, 90, 72, 95, 80, 85, 87, 78, 91)
# shapiro-wilk normality test
result1 <-shapiro.test(exam_score)
print(result1)
## 
##  Shapiro-Wilk normality test
## 
## data:  exam_score
## W = 0.96562, p-value = 0.8476
#The data is likely to follow a normal distribution.
#accept the null hypothesis

R Markdown

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:

before <- c(80, 85, 88, 90, 93, 95, 85, 87, 89, 92)  
after <- c(82, 86, 91, 93, 95, 97, 88, 90, 91, 94)
# paired t-test
result2 <- t.test(before, after , paired = TRUE)
print(result2)
## 
##  Paired t-test
## 
## data:  before and after
## t = -10.776, df = 9, p-value = 1.915e-06
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
##  -2.782829 -1.817171
## sample estimates:
## mean difference 
##            -2.3
#print the p-value in standard number foramt
formatted_p_value <- format(result2$p.value, scientific = FALSE)
print(formatted_p_value)
## [1] "0.000001914532"

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

data(iris)
# setosa와 versicolor의 Sepal.Length
setosa_sepal_length <- iris$Sepal.Length[iris$Species == "setosa"]
versicolor_sepal_length <- iris$Sepal.Length[iris$Species == "versicolor"]
# shapiro-wilk normality test
result3 <-shapiro.test(setosa_sepal_length)
print(result3)
## 
##  Shapiro-Wilk normality test
## 
## data:  setosa_sepal_length
## W = 0.9777, p-value = 0.4595
result4 <-shapiro.test(versicolor_sepal_length)
print(result4)
## 
##  Shapiro-Wilk normality test
## 
## data:  versicolor_sepal_length
## W = 0.97784, p-value = 0.4647
library(car)
## Loading required package: carData
#Leven's Test
levene_test <- leveneTest(Sepal.Length ~ Species, data = iris[iris$Species %in% c("setosa", "versicolor"), ])

levene_test
## Levene's Test for Homogeneity of Variance (center = median)
##       Df F value   Pr(>F)   
## group  1  8.1727 0.005196 **
##       98                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
library(car)
#Leven's Test
levene_test <- leveneTest(Sepal.Length ~ Species, data = iris[iris$Species %in% c("setosa", "versicolor"), ])

levene_test
## Levene's Test for Homogeneity of Variance (center = median)
##       Df F value   Pr(>F)   
## group  1  8.1727 0.005196 **
##       98                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.1     ✔ readr     2.2.0
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.3     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ✖ dplyr::recode() masks car::recode()
## ✖ purrr::some()   masks car::some()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
data(mtcars)
group_4<-mtcars %>% 
  filter(cyl == 4) %>% 
  select(mpg)
group_6<-mtcars %>% 
  filter(cyl == 6) %>% 
  select(mpg)
group_8<-mtcars %>% 
  filter(cyl == 8) %>% 
  select(mpg)
library(car)
levene_test <- leveneTest(mpg~factor(cyl), data=mtcars)
print(levene_test)
## Levene's Test for Homogeneity of Variance (center = median)
##       Df F value  Pr(>F)   
## group  2  5.5071 0.00939 **
##       29                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
welch_test_result1 <- t.test(group_4$mpg, group_6$mpg, var.equal = FALSE)

welch_test_result1
## 
##  Welch Two Sample t-test
## 
## data:  group_4$mpg and group_6$mpg
## t = 4.7191, df = 12.956, p-value = 0.0004048
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##   3.751376 10.090182
## sample estimates:
## mean of x mean of y 
##  26.66364  19.74286
welch_test_result2 <- t.test(group_8$mpg, group_6$mpg, var.equal = FALSE)

welch_test_result2
## 
##  Welch Two Sample t-test
## 
## data:  group_8$mpg and group_6$mpg
## t = -5.2911, df = 18.502, p-value = 4.54e-05
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -6.482789 -2.802925
## sample estimates:
## mean of x mean of y 
##  15.10000  19.74286
welch_test_result3 <- t.test(group_4$mpg, group_8$mpg, var.equal = FALSE)

welch_test_result3
## 
##  Welch Two Sample t-test
## 
## data:  group_4$mpg and group_8$mpg
## t = 7.5967, df = 14.967, p-value = 1.641e-06
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##   8.318518 14.808755
## sample estimates:
## mean of x mean of y 
##  26.66364  15.10000
data("sleep")
?sleep
view(sleep)
summary(sleep)
##      extra        group        ID   
##  Min.   :-1.600   1:10   1      :2  
##  1st Qu.:-0.025   2:10   2      :2  
##  Median : 0.950          3      :2  
##  Mean   : 1.540          4      :2  
##  3rd Qu.: 3.400          5      :2  
##  Max.   : 5.500          6      :2  
##                          (Other):8
# Convert group to factor with labels A and B
sleep <- sleep %>%
  mutate(group = factor(group, labels = c("A", "B")))
# Extract 'extra' values for each group (A and B)
A_group <- sleep %>%
  filter(group == "A") %>%
  select(extra)

B_group <- sleep %>%
  filter(group == "B") %>%
  select(extra)
# Paired t-test using the full dataset
t_test_AandB_trt1 <- t.test(A_group$extra, B_group$extra, var.equal = TRUE)

t_test_AandB_trt1
## 
##  Two Sample t-test
## 
## data:  A_group$extra and B_group$extra
## t = -1.8608, df = 18, p-value = 0.07919
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -3.363874  0.203874
## sample estimates:
## mean of x mean of y 
##      0.75      2.33
#There are no difference between group A and B as the P-value is more than 0.05.