Simple Linear Regression Using R
Simple Linear Regression Using R
2024-01-18
x <- c(2, 4, 2, 6, 4, 6, 8)
y <- c(4, 4, 2, 7, 5, 7, 9)
##
## Call:
## lm(formula = y ~ x)
##
## Residuals:
## 1 2 3 4 5 6 7
## 1.19231 -0.84615 -0.80769 0.11538 0.15385 0.11538 0.07692
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.7692 0.6940 1.108 0.318146
## x 1.0192 0.1384 7.364 0.000725 ***
## ---
## Signif. codes: 0 ’***’ 0.001 ’**’ 0.01 ’*’ 0.05 ’.’ 0.1 ’ ’ 1
##
## Residual standard error: 0.7545 on 5 degrees of freedom
## Multiple R-squared: 0.9156, Adjusted R-squared: 0.8987
## F-statistic: 54.23 on 1 and 5 DF, p-value: 0.0007254
1
Scatterplot and Regression Line
9
Data
Regression Line
8
7
6
y
5
4
3
2
2 3 4 5 6 7 8
• I added more descriptive arguments to the plot function to label the axes and the plot itself.
• The color of the data points is set to blue (col = "blue"), and the regression line is set to red (col
= "red").
• The point character is set to a solid circle (pch = 16), and the line width for the regression line is
increased (lwd = 2).
• I added a legend to the plot to label the data points and the regression line.