R Regression: Categorical Variables

From OnnoWiki
Jump to navigation Jump to search

Sumber: http://www.sthda.com/english/articles/40-regression-analysis/163-regression-with-categorical-variables-dummy-coding-essentials-in-r/#example-of-data-set



install.packages("car")
library(car)
library(tidyverse)

data("Salaries", package = "car")
# Inspect the data
sample_n(Salaries, 3)

# Compute the model
model <- lm(salary ~ sex, data = Salaries)
summary(model)$coef

# Buat Dummy Variable
contrasts(Salaries$sex)

# Set BASELINE Category
Salaries <- Salaries %>%
  mutate(sex = relevel(sex, ref = "Male"))

# OUTPUT Regression FIT
model <- lm(salary ~ sex, data = Salaries)
summary(model)$coef 

# BUAT CATEGORY LEBIH DARI DUA
res <- model.matrix(~rank, data = Salaries)
head(res[, -1])

# Predict SALARY
library(car)
model2 <- lm(salary ~ yrs.service + rank + discipline + sex,
             data = Salaries)
Anova(model2)
# Summary model
summary(model2)



Referensi


Pranala Menarik