0% found this document useful (0 votes)
17 views

Simple Linear Regression Using R

Uploaded by

tolorunoje91
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Simple Linear Regression Using R

Uploaded by

tolorunoje91
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Simple Linear Regression in R

2024-01-18

For more information, visit http://www.softdataconsult.com or contact us at [email protected].

x <- c(2, 4, 2, 6, 4, 6, 8)
y <- c(4, 4, 2, 7, 5, 7, 9)

# Fit a linear model


model <- lm(y ~ x)

# Display the summary of the linear model


summary(model)

##
## 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

# Plot the data


plot(x, y, col = "blue", pch = 16, main = "Scatterplot and Regression Line", xlab = "x", ylab = "y")

# Add the regression line to the plot


abline(model, col = "red", lwd = 2)

# Customize the plot if needed


legend("topleft", legend = c("Data", "Regression Line"), col = c("blue", "red"), pch = c(16, NA), lty =

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.

You might also like