Use the CO2 dataset in R

To get definitions of the columns type help(CO2)

str(CO2)
## Classes 'nfnGroupedData', 'nfGroupedData', 'groupedData' and 'data.frame':   84 obs. of  5 variables:
##  $ Plant    : Ord.factor w/ 12 levels "Qn1"<"Qn2"<"Qn3"<..: 1 1 1 1 1 1 1 2 2 2 ...
##  $ Type     : Factor w/ 2 levels "Quebec","Mississippi": 1 1 1 1 1 1 1 1 1 1 ...
##  $ Treatment: Factor w/ 2 levels "nonchilled","chilled": 1 1 1 1 1 1 1 1 1 1 ...
##  $ conc     : num  95 175 250 350 500 675 1000 95 175 250 ...
##  $ uptake   : num  16 30.4 34.8 37.2 35.3 39.2 39.7 13.6 27.3 37.1 ...
##  - attr(*, "formula")=Class 'formula'  language uptake ~ conc | Plant
##   .. ..- attr(*, ".Environment")=<environment: R_EmptyEnv> 
##  - attr(*, "outer")=Class 'formula'  language ~Treatment * Type
##   .. ..- attr(*, ".Environment")=<environment: R_EmptyEnv> 
##  - attr(*, "labels")=List of 2
##   ..$ x: chr "Ambient carbon dioxide concentration"
##   ..$ y: chr "CO2 uptake rate"
##  - attr(*, "units")=List of 2
##   ..$ x: chr "(uL/L)"
##   ..$ y: chr "(umol/m^2 s)"

Calculate means & standard deviations for 4 groups broken down by Type and Treatment

library(dplyr)
## Warning: package 'dplyr' was built under R version 3.4.4
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
Sum_Type_Trtmt <- CO2 %>% ungroup() %>% group_by(Type, Treatment)%>%
  summarise(mean_conc=mean(conc),
            std_conc=sd(conc),
            mean_uptake=mean(uptake),
            std_uptake=sd(uptake)) %>% ungroup()
Sum_Type_Trtmt
## # A tibble: 4 x 6
##   Type        Treatment  mean_conc std_conc mean_uptake std_uptake
##   <fct>       <fct>          <dbl>    <dbl>       <dbl>      <dbl>
## 1 Quebec      nonchilled       435     301.        35.3       9.60
## 2 Quebec      chilled          435     301.        31.8       9.64
## 3 Mississippi nonchilled       435     301.        26.0       7.40
## 4 Mississippi chilled          435     301.        15.8       4.06

Perform one-way tests twice: once for Type and once for Treatment

One-way test for Type
Oneway_Type <- lm(uptake~Type, data=CO2)
anova(Oneway_Type)
## Analysis of Variance Table
## 
## Response: uptake
##           Df Sum Sq Mean Sq F value    Pr(>F)    
## Type       1 3365.5  3365.5  43.519 3.835e-09 ***
## Residuals 82 6341.4    77.3                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

P-value less than alpha (0.05). Hence reject the null hypothesis. This concludes that the type affected the uptake rates.

One-way test for Treatment
Oneway_Treatment <- lm(uptake~Treatment, data=CO2)
anova(Oneway_Treatment)
## Analysis of Variance Table
## 
## Response: uptake
##           Df Sum Sq Mean Sq F value   Pr(>F)   
## Treatment  1  988.1  988.11  9.2931 0.003096 **
## Residuals 82 8718.9  106.33                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

P-value less than alpha (0.05). Hence reject the null hypothesis. This concludes that the treatment affected the uptake rates.

Perform a two-way test for Type and Treatment

Twoway_Type_Treatment <- lm(uptake~Type*Treatment, data=CO2)
anova(Twoway_Type_Treatment)
## Analysis of Variance Table
## 
## Response: uptake
##                Df Sum Sq Mean Sq F value    Pr(>F)    
## Type            1 3365.5  3365.5 52.5086 2.378e-10 ***
## Treatment       1  988.1   988.1 15.4164 0.0001817 ***
## Type:Treatment  1  225.7   225.7  3.5218 0.0642128 .  
## Residuals      80 5127.6    64.1                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

P-value less than alpha (0.05) for Type and Treatment individually. Hence reject the null hypothesis. This concludes that the Type and treatment individually affected the uptake rates.

P-value greater than alpha (0.05) for Type and Treatment together. Hence cannot reject the null hypothesis. This concludes that the combined effect of Type and treatment did not affect the uptake rates.

Use the mtcars dataset in R

Use the table() function with the following combinations

The variables vs and am
cars_vs_am <- with(mtcars, table(vs,am))
cars_vs_am
##    am
## vs   0  1
##   0 12  6
##   1  7  7
The variables gear and carb
cars_gear_carb <- with(mtcars, table(gear, carb))
cars_gear_carb
##     carb
## gear 1 2 3 4 6 8
##    3 3 4 3 5 0 0
##    4 4 4 0 4 0 0
##    5 0 2 0 1 1 1
The variables cyl and gear
cars_cyl_gear <- with(mtcars, table(cyl, gear))
cars_cyl_gear
##    gear
## cyl  3  4  5
##   4  1  8  2
##   6  2  4  1
##   8 12  0  2
For each of the three cases above guess what the results of a Chi-Squared analysis will be

I would guess that there is no dependency between any two of the above.

Perform a Chi-Squared analysis on the mtcars dataset for each of the three cases above

chisq.test(cars_vs_am)
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  cars_vs_am
## X-squared = 0.34754, df = 1, p-value = 0.5555
chisq.test(cars_gear_carb)
## Warning in chisq.test(cars_gear_carb): Chi-squared approximation may be
## incorrect
## 
##  Pearson's Chi-squared test
## 
## data:  cars_gear_carb
## X-squared = 16.518, df = 10, p-value = 0.08573
chisq.test(cars_cyl_gear)
## Warning in chisq.test(cars_cyl_gear): Chi-squared approximation may be
## incorrect
## 
##  Pearson's Chi-squared test
## 
## data:  cars_cyl_gear
## X-squared = 18.036, df = 4, p-value = 0.001214

P-value is greater than alpha (0.05) in case 1 and 2. Hence, null cannot be rejected. From this, it can be concluded that vs and am, gear and carb are indpendent of each other. In case 3, p-value is less than alpha(0.05). Hence, reject the null hypothesis and conclude that cyl and gear are dependent on one another.