R - Linear Regression
In statistics, linear regression (Linear Regression) is a regression analysis that models the relationship between one or more independent variables and a dependent variable using a least-squares function called the linear regression equation.
In simple terms, it is a statistical analysis method used to determine the quantitative relationship of mutual dependence between two or more variables.
In regression analysis, if only one independent variable and one dependent variable are included, and their relationship can be approximated by a straight line, this is called simple linear regression analysis. If the regression analysis includes two or more independent variables and the relationship between the dependent and independent variables is linear, it is called multiple linear regression analysis.
The mathematical equation of simple linear regression analysis:
y = ax + b
-
y is the value of the dependent variable.
-
x is the value of the independent variable.
-
a The difference between b are the parameters of the simple linear regression equation.
Next, we can create a prediction model of human height and weight:
- 1. Collect sample data: height and weight.
- 2. Use the lm() function to create a relationship model.
- 3. Find the coefficients from the created model and formulate the mathematical equation.
- 4. Obtain a summary of the relationship model to understand the mean error, i.e., the residual (the difference between the estimated and true values).
- 5. Use the predict() function to predict a person's weight.
Prepare data
The following is the height and weight data:
# 身高,单位 cm
151, 174, 138, 186, 128, 136, 179, 163, 152, 131
# Weight, unit kg
63, 81, 56, 91, 47, 57, 76, 72, 62, 48
The lm() function
In R, you can perform linear regression with the lm() function.
The lm() function is used to create a relationship model between independent and dependent variables.
The syntax of the lm() function is as follows:
lm(formula,data)
Parameter Description:
- formula - A symbolic formula representing the relationship between x and y.
- data - The data to which the formula is applied.
Create the relationship model and obtain the coefficients:
Example
Sample data
x
<-
c
(
151
,
174
,
138
,
186
,
128
,
136
,
179
,
163
,
152
,
131
)
y
<-
c
(
63
,
81
,
56
,
91
,
47
,
57
,
76
,
72
,
62
,
48
)
Passed to the lm() function
relation
<-
lm
(
y~x
)
(
relation
)
The output of the code above is:
Call:
lm(formula = y ~ x)
Coefficients:
(Intercept) x
-38.4551 0.6746
Use summary() Get a summary of the relationship model with the function:
Example
x
<-
c
(
151
,
174
,
138
,
186
,
128
,
136
,
179
,
163
,
152
,
131
)
y
<-
c
(
63
,
81
,
56
,
91
,
47
,
57
,
76
,
72
,
62
,
48
)
Passed to the lm() function
relation
<-
lm
(
y~x
)
(
summary
(
relation
)
)
The output of the code above is:
Call:
lm(formula = y ~ x)
Residuals:
Min 1Q Median 3Q Max
-6.3002 -1.6629 0.0412 1.8944 3.9775
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -38.45509 8.04901 -4.778 0.00139 **
x 0.67461 0.05191 12.997 1.16e-06 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 3.253 on 8 degrees of freedom
Multiple R-squared: 0.9548, Adjusted R-squared: 0.9491
F-statistic: 168.9 on 1 and 8 DF, p-value: 1.164e-06
The predict() function
The predict() function is used to predict values based on the model we built.
The syntax of the predict() function is as follows:
predict(object, newdata)
Parameter Description:
- object - The formula created by the lm() function.
- newdata - The values to predict.
In the following example, we predict a new weight value:
Example
Sample data
x
<-
c
(
151
,
174
,
138
,
186
,
128
,
136
,
179
,
163
,
152
,
131
)
y
<-
c
(
63
,
81
,
56
,
91
,
47
,
57
,
76
,
72
,
62
,
48
)
Passed to the lm() function
relation
<-
lm
(
y~x
)
Predict the weight for a height of 170cm
a
<-
data.frame
(
x
=
170
)
result
<-
predict
(
relation,a
)
(
result
)
The output of the code above is:
1
76.22869
We can also generate a chart:
Example
Sample data
x
<-
c
(
151
,
174
,
138
,
186
,
128
,
136
,
179
,
163
,
152
,
131
)
y
<-
c
(
63
,
81
,
56
,
91
,
47
,
57
,
76
,
72
,
62
,
48
)
relation
<-
lm
(
y~x
)
Generate a png image
png
(
file
=
"linearregression.png"
)
Generate the chart
plot
(
y,x,
col
=
"blue"
,main
=
"Height & Weight Regression"
,
abline
(
lm
(
x~y
)
)
,cex
=
1.3
,pch
=
16
,xlab
=
"Weight in Kg"
,ylab
=
"Height in cm"
)
The chart is as follows:

R Language Examples