# Set a seed for reproducibility
set.seed(123)

# Generate independent variable (predictor)
x <- 1:100

# Generate dependent variable (response) with some noise
# y = 2*x + 5 + random_noise
y <- 2 * x + 5 + rnorm(100, mean = 0, sd = 20)

# Create a data frame
my_data <- data.frame(x = x, y = y)

# View the first few rows of the data
head(my_data)

Explanation:

2. Fitting the Linear Regression Model

The core function in R for linear regression is lm().

# Fit the linear regression model
# The formula y ~ x means "model y as a function of x"
model <- lm(y ~ x, data = my_data)

Explanation:

3. Examining the Model Results

Once the model is fitted, you can inspect its results using several functions.

# Get a summary of the model
summary(model)

# Get the coefficients of the model
coefficients(model)

# Get the fitted (predicted) values
fitted(model)

# Get the residuals (differences between actual and fitted values)
residuals(model)

Explanation:

4. Visualizing the Model

It’s always a good practice to visualize your data and the fitted regression line.

# Plot the data and the regression line
plot(my_data$x, my_data$y, main = "Linear Regression Model",
     xlab = "Independent Variable (x)", ylab = "Dependent Variable (y)")
abline(model, col = "red", lwd = 2) # Add the regression line
legend("topleft", legend = paste("y =", round(coef(model)[2], 2), "* x +", round(coef(model)[1], 2)),
       col = "red", lty = 1, lwd = 2)

Explanation:

5. Making Predictions

You can use the fitted model to predict y values for new, unseen x values.

# Create new data for prediction
new_data <- data.frame(x = c(50, 110, 150))

# Predict y values for the new data
predictions <- predict(model, newdata = new_data)

# Display the predictions
print(predictions)

Explanation:

Example with Real Data (Conceptual)

If you had a CSV file named sales_data.csv with columns advertising_spend and sales, you would do this:

# 1. Load your data
sales_data <- read.csv("sales_data.csv")

# 2. Fit the model
sales_model <- lm(sales ~ advertising_spend, data = sales_data)

# 3. Examine results
summary(sales_model)

# 4. Visualize
plot(sales_data$advertising_spend, sales_data$sales,
     main = "Sales vs. Advertising Spend",
     xlab = "Advertising Spend", ylab = "Sales")
abline(sales_model, col = "blue", lwd = 2)

# 5. Predict for a new advertising spend value
new_ad_spend <- data.frame(advertising_spend = 5000)
predicted_sales <- predict(sales_model, newdata = new_ad_spend)
print(predicted_sales)