# Categorical Variables

Basic Analysis (linear regression + logistic regression)

# Categorical Variables

## Basic operation

### relevel: change the baseline of a categorical variable

#### basic syntax

relevel(factorVariable, ref="reference level")

• parameters
• factorVariable: a unordered factor variable in R
• reference level: a string value of one leve in the factor variable
• output
• the factor variable which uses ref as the reference level.

Example: mtcars dataset

Parameter Argument Meaning
catVar gear Number of forward gears
data mtcars Motor Trend Car Road Tests

Set 5 as the reference level.

Even if 5 is a number, 5 must be inside the quote marks.

## Descriptive

### table format xtabs()

basic syntax

• catVar1

Complicated for formula part.

Example: mtcars dataset

Parameter Argument Meaning
catVar vs V/S
catVar gear Number of forward gears
data mtcars Motor Trend Car Road Tests

xtabs(~ vs + gear, data = mtcars)

or

### list format count()

Example: mtcars dataset

Parameter Argument Meaning
catVar vs V/S
catVar gear Number of forward gears
data mtcars Motor Trend Car Road Tests

basic syntax

• dataset: a dataset
• catVector: a categorical vector

plyr::count(dataset, catVector)

## test

### n by m table: Chi-squared Test of Independence (Omnibus tests )

• table: count data
• correct: whether continuity correction is applied

chisq.test(table, correct = FALSE)

Example: mtcars dataset

Parameter Argument Meaning
catVar vs V/S
catVar gear Number of forward gears
data mtcars Motor Trend Car Road Tests

### n by m table: fisher test

#### bsic syntax

• table: count data

fisher.test(table)

default

• two-sided
• confidence level 0.95

#### fisher test for one category variable

Example: mtcars dataset

Parameter Argument Meaning
catVar vs V/S
catVar gear Number of forward gears
data mtcars Motor Trend Car Road Tests

#### fisher test for multiple category variables. compare pairwise

Example: mtcars dataset

Parameter Argument Meaning
catVar vs V/S
catVar am Transmission (0 = automatic, 1 = manual)
catVar gear Number of forward gears
catVar carb Number of carburetors
data mtcars Motor Trend Car Road Tests

#### pairwise fisher test for one category variable with adjustment

fisher test for two category variables & compair pairwise for one category variable

reporttools::pairwise.fisher.test()

basic syntax

pairwise.fisher.test(catVar, groupCatVar, p.adjust.method)

• catVar: a categorical variable
• groupCatVar: a categorical variable to group catVar
• p.adjust.method

Example: mtcars dataset

Parameter Argument Meaning
catVar vs V/S
catVar gear Number of forward gears
data mtcars Motor Trend Car Road Tests

#### pairwise fisher test for multiple categories variable grouped by one categorical variable with adjustment

Example: mtcars dataset

Parameter Argument Meaning
catVar vs V/S
catVar am Transmission (0 = automatic, 1 = manual)
catVar gear Number of forward gears
catVar carb Number of carburetors
data mtcars Motor Trend Car Road Tests

## Plots

### Barplots

#### basic syntax

• data: dataset
• catName: one categorical variable

#### barplot for one categorical variable

Example: mtcars dataset

Parameter Argument Meaning
catVar gear Number of forward gears
data mtcars Motor Trend Car Road Tests

or

#### barplot for two categorical variables

Example: mtcars dataset

Parameter Argument Meaning
catVar vs V/S
catVar gear Number of forward gears
data mtcars Motor Trend Car Road Tests