R Regression: Categorical Variables
Jump to navigation
Jump to search
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