R Set Fixed Intercept in Linear Regression Model (Example Code)
In this R post you’ll learn how to define a known intercept in a linear regression model.
Example Data
data(iris) # Load example data head(iris) # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 setosa # 2 4.9 3.0 1.4 0.2 setosa # 3 4.7 3.2 1.3 0.2 setosa # 4 4.6 3.1 1.5 0.2 setosa # 5 5.0 3.6 1.4 0.2 setosa # 6 5.4 3.9 1.7 0.4 setosa |
data(iris) # Load example data head(iris) # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 setosa # 2 4.9 3.0 1.4 0.2 setosa # 3 4.7 3.2 1.3 0.2 setosa # 4 4.6 3.1 1.5 0.2 setosa # 5 5.0 3.6 1.4 0.2 setosa # 6 5.4 3.9 1.7 0.4 setosa
my_mod <- lm(Sepal.Length ~ Sepal.Width, iris) # Estimating model with default intercept summary(my_mod) # Call: # lm(formula = Sepal.Length ~ Sepal.Width, data = iris) # # Residuals: # Min 1Q Median 3Q Max # -1.5561 -0.6333 -0.1120 0.5579 2.2226 # # Coefficients: # Estimate Std. Error t value Pr(>|t|) # (Intercept) 6.5262 0.4789 13.63 <2e-16 *** # Sepal.Width -0.2234 0.1551 -1.44 0.152 # --- # Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 # # Residual standard error: 0.8251 on 148 degrees of freedom # Multiple R-squared: 0.01382, Adjusted R-squared: 0.007159 # F-statistic: 2.074 on 1 and 148 DF, p-value: 0.1519 |
my_mod <- lm(Sepal.Length ~ Sepal.Width, iris) # Estimating model with default intercept summary(my_mod) # Call: # lm(formula = Sepal.Length ~ Sepal.Width, data = iris) # # Residuals: # Min 1Q Median 3Q Max # -1.5561 -0.6333 -0.1120 0.5579 2.2226 # # Coefficients: # Estimate Std. Error t value Pr(>|t|) # (Intercept) 6.5262 0.4789 13.63 <2e-16 *** # Sepal.Width -0.2234 0.1551 -1.44 0.152 # --- # Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 # # Residual standard error: 0.8251 on 148 degrees of freedom # Multiple R-squared: 0.01382, Adjusted R-squared: 0.007159 # F-statistic: 2.074 on 1 and 148 DF, p-value: 0.1519
Example: Set Fixed Intercept in Linear Regression Model
my_intercept <- 5 # Estimating model with fixed intercept my_mod_fixed <- lm(I(Sepal.Length - my_intercept) ~ 0 + Sepal.Width, iris) summary(my_mod_fixed) # Call: # lm(formula = I(Sepal.Length - my_intercept) ~ 0 + Sepal.Width, # data = iris) # # Residuals: # Min 1Q Median 3Q Max # -1.49788 -0.75825 0.05212 0.65371 2.00850 # # Coefficients: # Estimate Std. Error t value Pr(>|t|) # Sepal.Width 0.26596 0.02248 11.83 <2e-16 *** # --- # Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 # # Residual standard error: 0.8501 on 149 degrees of freedom # Multiple R-squared: 0.4845, Adjusted R-squared: 0.481 # F-statistic: 140 on 1 and 149 DF, p-value: < 2.2e-16 |
my_intercept <- 5 # Estimating model with fixed intercept my_mod_fixed <- lm(I(Sepal.Length - my_intercept) ~ 0 + Sepal.Width, iris) summary(my_mod_fixed) # Call: # lm(formula = I(Sepal.Length - my_intercept) ~ 0 + Sepal.Width, # data = iris) # # Residuals: # Min 1Q Median 3Q Max # -1.49788 -0.75825 0.05212 0.65371 2.00850 # # Coefficients: # Estimate Std. Error t value Pr(>|t|) # Sepal.Width 0.26596 0.02248 11.83 <2e-16 *** # --- # Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 # # Residual standard error: 0.8501 on 149 degrees of freedom # Multiple R-squared: 0.4845, Adjusted R-squared: 0.481 # F-statistic: 140 on 1 and 149 DF, p-value: < 2.2e-16
Related Articles
In addition, you could read the other tutorials of my website.