![]() |
VOOZH | about |
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.
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.
Output:
[5]TRUE
Now do the same things for strings.
Output:
[1]"small" "big" "medium" "small" "small" "big" "medium" "medium" "big" [10]TRUE [12]FALSE
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
Output:
[1] small big medium small small big medium medium big Levels: small < medium < big
Another way to make a factor ordered is:
NA 0.7
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.
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:
R creates dummy variables automatically with the following code:
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.