Write a R program to fit a linear regression

1. Introduction

This R program demonstrates how to fit a linear regression model to a dataset. Linear regression is used to model the relationship between a dependent variable and one or more independent variables.

2. Program Steps

1. Create a dataset with independent and dependent variables.

2. Fit a linear regression model to the data.

3. Print the summary of the fitted model.

3. Code Program

# Step 1: Create a dataset
# Example dataset with independent variable (x) and dependent variable (y)
x <- c(1, 2, 3, 4, 5)  # Independent variable
y <- c(2, 4, 5, 4, 5)  # Dependent variable

# Step 2: Fit a linear regression model
model <- lm(y ~ x)

# Step 3: Print the summary of the fitted model
print("Summary of the fitted linear regression model:")
summary(model)

Output:

Summary of the fitted linear regression model:
[Output of the linear regression model summary including coefficients, residuals, R-squared value, etc.]

Explanation:

1. x <- c(1, 2, 3, 4, 5), y <- c(2, 4, 5, 4, 5): Defines the independent (x) and dependent (y) variables.

2. lm(y ~ x): Fits a linear regression model to the data, with y as the dependent variable and x as the independent variable.

3. summary(model): Prints a summary of the fitted model, which includes coefficients, residuals, R-squared value, and other statistical measures that describe the model's performance.


Comments