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)"
Type=lm(uptake~Type, data=CO2)
anova(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
Treatment=lm(uptake~Treatment, data=CO2)
anova(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
In one way test for type, p-value = 3.834686110^{-9}. Therefore, we could reject the null hypothesis. This means that the origin of the plant made impacted the uptake rates.
In one way test for treatment, p-value = 0.0030957, Therefore, we could reject the null hypothesis. This means the type made an impact on the uptake rates.
TypeandTreatment=lm(uptake~Type*Treatment, data=CO2)
anova(TypeandTreatment)
## 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 for both Type and Treatment is small,therefore we reject the null hypothesis and conclude that Type as well as Treatment made impact on the uptake rate. However, the p-value for the interaction term is 0.06, therefore we cannot reject the null and conclude that the interaction between Type and Treatment did not make a significant impact on the uptake rates in Grass Plants.
Use the table() function with the following combinations The variables vs and am The variables gear and carb The variables cyl and gear
TableVsAm=with(mtcars, table(vs,am))
TableGearCarb=with(mtcars, table(gear, carb))
TableCylGear=with(mtcars, table(cyl, gear))
TableVsAm
## am
## vs 0 1
## 0 12 6
## 1 7 7
TableGearCarb
## 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
TableCylGear
## 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? Ignore warnings for low values in the cells and perform a Chi-Squared analysis on the mtcars dataset for each of the three cases above
I guess that all these variables are independent.
chisq.test(TableVsAm)
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: TableVsAm
## X-squared = 0.34754, df = 1, p-value = 0.5555
chisq.test(TableGearCarb)
## Warning in chisq.test(TableGearCarb): Chi-squared approximation may be
## incorrect
##
## Pearson's Chi-squared test
##
## data: TableGearCarb
## X-squared = 16.518, df = 10, p-value = 0.08573
chisq.test(TableCylGear)
## Warning in chisq.test(TableCylGear): Chi-squared approximation may be
## incorrect
##
## Pearson's Chi-squared test
##
## data: TableCylGear
## X-squared = 18.036, df = 4, p-value = 0.001214
Based on the tests above, p-value of the first 2 tests are higher than 0.05, therefore we will not reject the null and conclude that vs and am, gear and carb are independent with each other. However, p-value for cyl and Gear is 0.0012141, therefore, we will reject the null and conclude that the cyl and gear are not independent with each other. Overall, Cyl and Gear is not independent and the other two are independent.