---
title: "MeanCompLabAssignment"
author: "Jia-Shen"
date: "2023-09-25"
output: html_document
---
# Lab Assignment 2
```{r libraries, include=F}
rm(list = ls())
library(moments) #package for skewness
library(knitr) #package for making tables (kable)
library(tidyverse) #multiple packages for data wrangling
library(gt) # a package to make tables
library(lubridate) # a package to manipulate dates
```
```{r}
PEC <- read.csv('../input/PEC.csv')
```
```{r}
glimpse(PEC)
PEC$year <- as.factor(PEC$year)
```
```{r}
PEC <- PEC %>%
group_by(canton, year, high_adopt) %>%
summarise(annual_energy = sum(kwh_total),
annual_hosp = sum(total_hosp)) %>%
subset(year == 2015 | year == 2020)
high_adoption <- PEC %>%
subset(high_adopt == 1)
low_adoption <- PEC %>%
subset(high_adopt == 0)
```
```{r exploration}
p <- ggplot(high_adoption, aes(x = annual_energy, fill = `year`))+
geom_histogram(col="black")+
scale_fill_manual(values=c("royalblue", "gray"))
p
p + facet_grid(`year` ~ .)
```
```{r}
PEC <- PEC %>%
mutate(annual_energy.log = log(annual_energy))
high_adoption <- PEC %>%
subset(high_adopt == 1)
low_adoption <- PEC %>%
subset(high_adopt == 0)
```
```{r}
p2 <- ggplot(high_adoption, aes(x = annual_energy.log, fill = `year`))+
geom_histogram(col="black")+
scale_fill_manual(values=c("royalblue", "gray"))
p2 + facet_grid(`year` ~ .)
```
```{r}
p3 <- ggplot(low_adoption, aes(x = annual_energy.log, fill = `year`))+
geom_histogram(col="black")+
scale_fill_manual(values=c("royalblue", "gray"))
p3 + facet_grid(`year` ~ .)
```
```{r}
C1 <- t.test(annual_energy.log ~ `high_adopt`, PEC)
C2 <- t.test(annual_energy.log ~ `year`, high_adoption, paired=TRUE)
C3 <- t.test(annual_energy.log ~ `year`, low_adoption, paired=TRUE)
C5a <- t.test(annual_hosp ~ `year`, high_adoption, paired=TRUE)
C5b <- t.test(annual_hosp ~ `year`, low_adoption, paired=TRUE)
```
```{r}
C1
C2
C3
C5a
C5b
```
```{r}
high.wide <- high_adoption %>%
select(canton, year, annual_energy) %>%
pivot_wider(names_from = year, values_from = annual_energy) %>%
glimpse()
head(high.wide)
low.wide <- low_adoption %>%
select(canton, year, annual_energy) %>%
pivot_wider(names_from = year, values_from = annual_energy) %>%
glimpse()
head(low.wide)
```
``` {r}
npc1 <- wilcox.test(high.wide$`2020`, high.wide$`2015`, paired = TRUE, alternative = "greater")
npc2 <- wilcox.test(low.wide$`2020`, low.wide$`2015`, paired = TRUE, alternative = "greater")
```
```{r}
npc1
npc2
```