# In-Class Acitivity

We are going to use mtcars data included in R by default. We will load and print the mtcars data.

# 1. Loading
data(mtcars)

# 2. Print
head(mtcars)

## Check Data

Check the number of rows and columns. (Learn more about Data Types and Objects in R: https://msu.edu/~lixue/geo866/lab02/data_type.html)

# Display the internal structure of an R object.
str(mtcars)

# Check the dimension of an object
dim(mtcars)

# Number of rows (observations)
nrow(mtcars)

# Number of columns (variables)
ncol(mtcars)

# Bring up a help file
help(mtcars)

# Aquestion mark is a shorcut for the "help" function.
?mtcars

## Form a Contingency Table

We will learn how to conduct Chi-Square Test on the gear (Number of forward gears) and cyl (Number of cylinders) columns in the mtcars data set.

First, let’s form the contengency table. The table function returns a contingency table of the counts at each combination of factor labels.

table(mtcars$gear, mtcars$cyl)

## Conduct Chi-Squared Test

Now, we will conduct the chi-squared test using the chisq.test() function. We also set correct=FALSE to turn off Yates’ continuity correction.

chisq.test(mtcars$gear, mtcars$cyl, correct=FALSE)

## Get Expected Counts

To get a table of expected counts, type this.

xsq <- chisq.test(mtcars$gear, mtcars$cyl, correct=FALSE)
xsq$expected ## Conduct Fisher’s Exact Test Chi-square test is used to the the association between two categorical variables when the cell sizes are expected to be large. Fisher’s Exact test is used when sample size is small (or you have expected cell sizes < 5). fisher.test(mtcars$gear, mtcars$cyl) # Assignment 7 (Week 11) ## Read Data We will use build-in data set survey in the MASS package. # Install the MASS package if you haven't (remove # from the code below) #install.packages("MASS") # Load the MASS Package library(MASS) We will load and print the survey data. # 1. Loading data(survey) # 2. Print head(survey) ## Check Data Check the number of rows and columns. # Display the internal structure of an R object. str(survey) # Check the dimension of an object dim(survey) # Number of rows (observations) nrow(survey) # Number of columns (variables) ncol(survey) ## Form a Contingency Table Now, we will conduct Chi-Square Test on the Smoke (How much the student smokes) and Exer (How often the student exercise) columns in the mtcars data set. First, let’s form the contengency table. table(survey$Smoke, survey$Exer)  ## Conduct Chi-Squared Test Nest, we will conduct the chi-squared test using the chisq.test() function. We also set correct=FALSE to turn off Yates’ continuity correction. chisq.test(survey$Smoke, survey$Exer, correct=FALSE) ## Combine columns The warning message above is due to the small cell values in the contingency table. We can combine the second and third columns to avoid the warning sign. First, we will save the contingency table named tbl. # Save the contingency table as tbl tbl <- table(survey$Smoke, survey\$Exer)
tbl

# We can apply the chisq.test function to the contingency table tbl
chisq.test(tbl)

Next, combine the second and third columns of tbl. Save it in a new table named ctbl.

# Combine the second and third columns
ctbl <- cbind(tbl[,"Freq"], tbl[,"None"] + tbl[,"Some"])
ctbl

We can apply the chisq.test function to the contingency table ctbl

chisq.test(ctbl)

November 6, 2019