Linear regression model with manual reference factor level
To specify the manual reference factor level in the R Language, we will use the relevel() function. The relevel() function is used to reorder the factor vector so that the level specified by the user is first and others are moved down. The relevel() function takes factor vector and reference level as argument and returns the factor vector with levels reordered according to the reference level set by the user.
Syntax:
relevel( factor_vector, ref )
Parameter:
- factor_vector: determines the factor vector which is to be reordered.
- ref: determines the reference level according to which factor is to be reordered.
Example:
Here is a basic linear regression model with the factor reference level set to 4.
R
# create sample data frame x <- sample (1:7, 500, replace = TRUE ) y <- round (x + rnorm (500), 3) x <- as.factor (x) sample_data <- data.frame (x, y) # refactor reference level sample_data$x <- relevel (sample_data$x, ref = 4) # create linear model linear_model <- lm ( y~x, sample_data) # print summary of linear model summary (linear_model) |
Output:
Specify Reference Factor Level in Linear Regression in R
In this article, we will discuss how to specify Reference Factor Level in Linear Regression in the R Programming Language.
In a basic linear regression model in the R Language, by default, the reference category 1 is used for the factor variable. But sometimes we need to manually set the reference factor level in the linear regression model. To do so we use the relevel() function of the R Language. The relevel() function is used to reorder levels of a factor vector. The levels of a factor vector are re-ordered so that the level specified by the user is first and the others are moved down one step.