R语言方差分析


---
title: "方差分析 analysis"
author:
  - Liu tiezhu
date: '2022-08-29'
documentclass: ctexart
keywords:
  - 中文
  - R Markdown
output:
  rticles::ctex:
    fig_caption: yes
    number_sections: yes
    toc: yes
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Analysis with R 
- Explore
- clean
- Manipulate
- Describe and summarise
- Analyse
### Analyse your data
## ANOVA coding
``` {r}
library(tidyverse)
library (patchwork)
library(gapminder)  #life expect in difference area
library(forcats)
#data()
head(gapminder)
# Create a data set to work with
gapdata <-  gapminder %>%
filter(year ==2007&
continent %in% c("Americas","Europe","Asia")) %>%
select(continent,lifeExp)
#Take a look at the distribution of means
gapdata %>%
group_by(continent) %>%
summarise(Mean_life=mean(lifeExp)) %>%
arrange(Mean_life)
#Research question:
# Is the life expectancy  in these three continents
# Research question: Is the life expectancy in these three continents different
# Hypothesis testing: HO:Mean life expectancy is the same
#                     HI:Mean life expectancy is not the same
# observation:
# Difference in mean is observed in the sample data,but is this statistically 
#  significant (alpha 0.05)    
#  Create ANOVA mode 1  
gapdata  %>%
aov(lifeExp ~ continent,data =.) %>%
summary()
aov_model <- gapdata  %>%
aov(lifeExp ~ continent,data =.)

# Is this significance being driven by a particular continent?  
gapdata %>%
aov(lifeExp ~ continent,data = .) %>%
TukeyHSD() %>%
plot()
TukeyHSD(aov_model)

#The difference between Asia and the Americas
#has an adjusted p value of 0.14 (not significant)
#and a 95%cI that overlaps 0

```