library(tidyverse)
## Loading tidyverse: tibble
## Loading tidyverse: tidyr
## Loading tidyverse: readr
## Loading tidyverse: purrr
## Loading tidyverse: dplyr
## Conflicts with tidy packages ----------------------------------------------
## filter(): dplyr, stats
## lag():    dplyr, stats
library(RCurl)
## Loading required package: bitops
## 
## Attaching package: 'RCurl'
## The following object is masked from 'package:tidyr':
## 
##     complete
ward<-read_csv(getURL("https://raw.githubusercontent.com/chrischizinski/SNR_R_Group/master/data/ExperimentalDesignData/chpt3/ward.csv"))

head(ward)
## # A tibble: 6 × 2
##     ZONE  EGGS
##    <chr> <int>
## 1 Mussel    11
## 2 Mussel     8
## 3 Mussel    18
## 4 Mussel    10
## 5 Mussel     9
## 6 Mussel    13
ward_summ<-ward %>% 
    group_by(ZONE) %>% 
    summarize(N = n(),
              Meaneggs = mean(EGGS),
              Medianeggs = median(EGGS),
              SDeggs = sd(EGGS)) %>% 
    mutate(SEeggs = SDeggs/sqrt(N),
           CI_hi = Meaneggs + 1.96* SEeggs,
           CI_lo = Meaneggs - 1.96* SEeggs)

ggplot(data = ward) + 
  geom_boxplot(aes(x = ZONE, y = EGGS)) + 
  theme_bw()

ggplot(data = ward) + 
  geom_violin(aes(x = ZONE, y = EGGS, fill = ZONE)) + 
  theme_bw()

data.frame(ZONE = ward_summ$ZONE, CV = ward_summ$SDeggs/ward_summ$Meaneggs)
##     ZONE        CV
## 1 Littor 0.2327708
## 2 Mussel 0.2037969
t.test(EGGS ~ ZONE, data = ward, var.equal = TRUE)
## 
##  Two Sample t-test
## 
## data:  EGGS by ZONE
## t = -5.3899, df = 77, p-value = 0.0000007457
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -3.63511 -1.67377
## sample estimates:
## mean in group Littor mean in group Mussel 
##             8.702703            11.357143
t.test(EGGS ~ ZONE, data = ward, var.equal = FALSE)
## 
##  Welch Two Sample t-test
## 
## data:  EGGS by ZONE
## t = -5.4358, df = 76.998, p-value = 0.0000006192
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -3.626816 -1.682065
## sample estimates:
## mean in group Littor mean in group Mussel 
##             8.702703            11.357143
furness<-read_csv(getURL("https://raw.githubusercontent.com/chrischizinski/SNR_R_Group/master/data/ExperimentalDesignData/chpt3/furness.csv"))

head(furness)
## # A tibble: 6 × 2
##      SEX METRATE
##    <chr>   <dbl>
## 1   Male  2950.0
## 2 Female  1956.1
## 3   Male  2308.7
## 4   Male  2135.6
## 5   Male  1945.6
## 6 Female  1490.5
furn_summ<-furness %>% 
    group_by(SEX) %>% 
    summarize(N = n(),
              Meanmetrate = mean(METRATE),
              Medianmetrate = median(METRATE),
              SDmetrate = sd(METRATE)) %>% 
    mutate(SEmetrate = SDmetrate/sqrt(N),
           CI_hi = Meanmetrate + 1.96* SEmetrate,
           CI_lo = Meanmetrate - 1.96* SEmetrate)

ggplot(data = furness) + 
  geom_boxplot(aes(x = SEX, y = METRATE)) + 
  theme_bw()

ggplot(data = furness) + 
  geom_violin(aes(x = SEX, y = METRATE, fill = METRATE)) + 
  theme_bw()

data.frame(SEX = furn_summ$SEX, CV = furn_summ$SDmetrate/furn_summ$Meanmetrate)
##      SEX        CV
## 1 Female 0.3274656
## 2   Male 0.5719318
t.test(METRATE ~ SEX, data = furness, var.equal = TRUE)
## 
##  Two Sample t-test
## 
## data:  METRATE by SEX
## t = -0.70086, df = 12, p-value = 0.4968
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -1143.3057   586.7891
## sample estimates:
## mean in group Female   mean in group Male 
##             1285.517             1563.775
t.test(METRATE ~ SEX, data = furness, var.equal = FALSE)
## 
##  Welch Two Sample t-test
## 
## data:  METRATE by SEX
## t = -0.77317, df = 10.468, p-value = 0.4565
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -1075.3208   518.8042
## sample estimates:
## mean in group Female   mean in group Male 
##             1285.517             1563.775
elgar<-read_csv(getURL("https://raw.githubusercontent.com/chrischizinski/SNR_R_Group/master/data/ExperimentalDesignData/chpt3/elgar.csv"))

head(elgar)
## # A tibble: 6 × 5
##    PAIR VERTDIM HORIZDIM VERTLIGH HORIZLIG
##   <chr>   <int>    <int>    <int>    <int>
## 1     K     300      295       80       60
## 2     M     240      260      120      140
## 3     N     250      280      170      160
## 4     O     220      250       90      120
## 5     P     160      160      150      180
## 6     R     170      150      110       90
names(elgar) <-c("PAIR", "VERT_DIM", "HORIZ_DIM", "VERT_LIGH","HORIZ_LIGH")

elgar %>% 
  gather(type, value, -PAIR) %>% 
  separate(type, c("DIMEN","L_COND"), sep = "_") %>% 
  group_by(DIMEN,L_COND) %>% 
  summarize(N = n(),
              Meanradius = mean(value),
              Medianradius = median(value),
              SDradius = sd(value)) %>% 
    mutate(SEradius = SDradius/sqrt(N),
           CI_hi = Meanradius + 1.96* SEradius,
           CI_lo = Meanradius - 1.96* SEradius)
## Source: local data frame [4 x 9]
## Groups: DIMEN [2]
## 
##   DIMEN L_COND     N Meanradius Medianradius SDradius SEradius    CI_hi
##   <chr>  <chr> <int>      <dbl>        <int>    <dbl>    <dbl>    <dbl>
## 1 HORIZ    DIM    17   207.3529          210 60.21103 14.60332 235.9754
## 2 HORIZ   LIGH    17   161.1765          160 65.46777 15.87827 192.2979
## 3  VERT    DIM    17   198.2353          190 60.54289 14.68381 227.0156
## 4  VERT   LIGH    17   177.6471          160 73.86892 17.91585 212.7621
## # ... with 1 more variables: CI_lo <dbl>
elgar %>% 
  gather(type, value, -PAIR) %>% 
  separate(type, c("DIMEN","L_COND"), sep = "_") %>% 
  ggplot() +
  geom_violin(aes(x = L_COND, y = value, fill = L_COND)) +
  facet_wrap(~DIMEN, ncol = 1) +
  theme_bw()

elgar %>% 
  gather(type, value, -PAIR) %>% 
  separate(type, c("DIMEN","L_COND"), sep = "_") %>% 
  mutate(DIMEN2 = factor(DIMEN, labels = c("beta", "sqrt(x,y)"))) %>% 
  ggplot() +
  geom_violin(aes(x = L_COND, y = value, fill = L_COND)) +
  facet_wrap(~DIMEN, ncol = 2, labeller = label_bquote(cols = alpha^.(DIMEN))) +
  theme_bw()

head(elgar)
## # A tibble: 6 × 5
##    PAIR VERT_DIM HORIZ_DIM VERT_LIGH HORIZ_LIGH
##   <chr>    <int>     <int>     <int>      <int>
## 1     K      300       295        80         60
## 2     M      240       260       120        140
## 3     N      250       280       170        160
## 4     O      220       250        90        120
## 5     P      160       160       150        180
## 6     R      170       150       110         90
t.test(elgar$VERT_DIM, elgar$VERT_LIGH, paired = TRUE)
## 
##  Paired t-test
## 
## data:  elgar$VERT_DIM and elgar$VERT_LIGH
## t = 0.96545, df = 16, p-value = 0.3487
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -24.61885  65.79532
## sample estimates:
## mean of the differences 
##                20.58824
t.test(elgar$HORIZ_DIM, elgar$HORIZ_LIGH, paired = TRUE)
## 
##  Paired t-test
## 
## data:  elgar$HORIZ_DIM and elgar$HORIZ_LIGH
## t = 2.1482, df = 16, p-value = 0.04735
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##   0.6085725 91.7443687
## sample estimates:
## mean of the differences 
##                46.17647