How to Include Factors in Regression using R Programming? - GeeksforGeeks

10 Nov, 2020
Categorical variables (also known as a factor or qualitative variables) are variables that classify observational values into groups. They are either string or numeric are called factor variables in statistical modeling. Saving normal string variables as factors save a lot of memory. Factors can also be stored as level or label variables. They have a limited number of different values, called levels. For example, the gender of individuals is a categorical variable that can take two levels: Male or Female. Regression requires numeric variables. So, when a researcher wants to include a categorical variable in a regression model, steps are needed to make the results interpretable. Let’s see all this with a code example in the R language.
First of all, let’s create a sample data set.  
R
# creating samplesamp <- sample(0:1, 20, replace = TRUE)samp
Output:
[1] 1 1 0 1 1 1 0 0 1 0 0 1 1 1 1 1 1 1 0 1



Converting the numbers set as factors.
R
samp <- sample(0:1, 20, replace = TRUE)  # converting sampleto factorssamp1 <- factor(samp)  # to find if its a factor lets use is.factor()is.factor(samp1)
Output:
[5]TRUE



Now do the same things for strings.
R
# creating string samplestr1 <- c("small", "big", "medium", "small", "small",          "big", "medium", "medium", "big")str1  # will show output of stringf.str1<-factor(str1)  # check if f.str1 is factor or notis.factor(f.str1)  # check if str1 is factor or notis.factor(str1)
Output:
[1]"small"  "big"    "medium" "small"  "small"  "big"    "medium" "medium" "big"   
[10]TRUE
[12]FALSE



R
# creating sample with labelslab <- factor(samp1, labels = c("sweet", "bitter"))lab
Output:
bitter bitter sweet  bitter bitter bitter sweet  sweet  bitter sweet 
[11] sweet  bitter bitter bitter bitter bitter bitter bitter sweet  bitter
Levels: sweet bitter



R
str1 <- c("small", "big", "medium", "small", "small",          "big", "medium", "medium", "big")  # ordering the factors w.r.t levelsorder <- ordered(str1,                  levels = c("small", "medium", "big"))orderf.order <- factor(order)
Output:
[1] small  big    medium small  small  big    medium medium big   
Levels: small < medium < big



Another way to make a factor ordered is:
R
# another way to order f.order = factor(str1,                  levels = c("small", "medium", "big"),                 ordered = TRUE)
R
mean(samp1)  # shows NA has outputmean(as.numeric(levels(samp1)[samp1]))
NA
0.7



R
f.new <- f.order[f.order != "small"]f.new
Output:
[1] big    medium big    medium medium big   
Levels: small < medium < big



Consider the experiment as the hours the students stay at school during fests.
R
# consider a dataframe studentstudent <- data.frame(    # id of students    id = c (1:5),         # name of students      name = c("Payal", "Dan", "Misty", "Ryan", "Gargi"),        # gender of students    gender = c("F", "M", "F", "M", "F"),        # gender represented in numbers F-1,M-0    gender_num = c(1, 0, 1, 0, 1),        # the hours students stay at fests    hours = c(2.5, 4, 5.3, 3, 2.2))student
Output:
  id  name   gender gender_num hours
1  1 Payal      F          1   2.5
2  2   Dan      M          0   4.0
3  3 Misty      F          1   5.3
4  4  Ryan      M          0   3.0
5  5 Gargi      F          1   2.2



The regression equation is
 y = b0 + b1*x 
Where 
y: output variable predicted on the basis of a predictor variable (x),  
b0 + b1: beta coefficients, representing the intercept and the slope, respectively.
b0 + b1: if a student is male, b0: if a student is female. The coefficients can be interpreted as follow:
b0 is the average hours’ female students stayed at fests,
b0 + b1 is the average hours’ male students stayed at fests and
b1 is the average difference in hours between male and female students.
 R creates dummy variables automatically with the following code:
R
# making the regression modelmodel <- lm(hours ~ gender, data = student) summary(model)$coef
Output:
            Estimate Std. Error   t value   Pr(>|t|)
(Intercept) 3.3333333  0.8397531 3.9694209 0.02857616
genderM     0.1666667  1.3277662 0.1255241 0.90804814



The estimated value for F students is 3.3333333 and for M student is 0.16666667. The Pr value of M students and F student is not so significant and only 0.90-0.02 ~ 0.9,i.e, there’s no actual evidence that M students stay more hours than females.
Picked
R regression
R-Factors
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Control Statements in R Programming
Change Color of Bars in Barchart using ggplot2 in R
Data Visualization in R
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
Logistic Regression in R Programming
How to change Colors in ggplot2 Line Plot in R ?
How to filter R DataFrame by values in a column?
Linear Discriminant Analysis in R Programming
R - Operators