This report details the process of simulating a dataset, performing a
linear regression analysis, and visualizing the relationship between two
variables, x and y We will walk through the
code step-by-step to understand its purpose and output.
The first line of code ensures that our analysis is reproducible.
Explanation:
set.seed() initializes R’s random number generator.
Using the same seed value (683475) guarantees that every
time we run this script, we will generate the exact same sequence of
random numbers. This is crucial for creating reproducible research and
sharing analyses where the results need to be consistent.
We create our first variable, x, by drawing random
values from a normal distribution.
Explanation:
rnorm(n) is the function for generating random numbers
from a standard normal distribution (mean = 0, standard deviation =
1).
The argument 300 tells R to generate 300 such
values.
The result is a numeric vector of 300 values stored in the object
x.
The variable y is created to have a linear relationship
with x.
Explanation: This line constructs y based on the classical linear model: \(y = \beta_0 + \beta_1x + \epsilon\).
rnorm(300, 2, 0.5): This generates the random error term
(\(\epsilon\)) from a normal
distribution with a mean of 2 and a standard deviation of 0.5. The mean
of 2 here acts as our model’s intercept (\(\beta_0\)).
0.3 * x: This is the systematic part of the model, where 0.3 is the true population slope (\(\beta_1\)). It defines how much y changes for a one-unit change in x.
The Combined Effect: So, the final y variable is equal
to: an intercept of 2, plus some random noise, plus a component that
depends linearly on x with a slope of 0.3.
We now combine our two vectors into a structured data frame.
Explanation:
A data.frame is the primary data structure in R for statistical analysis. It’s like a spreadsheet where columns are variables and rows are observations.
my_data is now a data frame with two columns: x and y.
This is the required format for most modeling functions in R, including
lm().
We use the lm() function to fit a linear model to our
data.
##
## Call:
## lm(formula = y ~ x, data = my_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.45248 -0.32739 -0.00737 0.33125 1.33657
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.03033 0.02817 72.08 <2e-16 ***
## x 0.28675 0.02623 10.93 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.4856 on 298 degrees of freedom
## Multiple R-squared: 0.2862, Adjusted R-squared: 0.2838
## F-statistic: 119.5 on 1 and 298 DF, p-value: < 2.2e-16
Explanation:
lm() is the function for linear models.
The formula y ~ x specifies the model: we want to
predict y as a function of x.
data = my_data tells the function where to find the
variables x and y
summary() provides a comprehensive summary of the fitted
model.
Interpreting the Output:
Coefficients (Estimate):
(Intercept): The estimated model intercept is r
round(model_summary$coefficients[1, 1], 3). This is close
to the true value of 2 we used in the simulation.
x: The estimated slope for x is r
round(model_summary$coefficients[2, 1], 3). This is very
close to the true value of 0.3 we used in the simulation.
P-values: The very small p-values (<2e-16) for both coefficients
indicate that the relationship between x and y
is statistically significant, and the intercept is significantly
different from zero.
R-squared: The Multiple R-squared value of r round(model_summary\(r.squared, 3) means that approximately r round(model_summary\) r.squared * 100, 1)% of the variation in y can be explained by its linear relationship with x.
Finally, we create a scatter plot to visualize the data and the fitted regression line.
library("ggplot2")
ggplot(my_data, aes(x = x, y = y)) +
geom_point(alpha = 0.6) + # Scatter plot of points
geom_smooth(method = "lm", color = "red", se = TRUE) + # Adds regression line and confidence band
labs(title = "Scatter Plot of Y versus X",
subtitle = "With Linear Regression Line and 95% Confidence Interval",
x = "Predictor Variable (X)",
y = "Response Variable (Y)") +
theme_minimal()Explanation of the ggplot2 code:
ggplot(my_data, aes(x = x, y = y)): Initializes the plot
using our my_data data frame and sets x and y as the aesthetic
mappings.
geom_point(alpha=0.6): Adds the scatter plot layer. The
alpha argument makes the points semi-transparent, which helps with
visualizing overlapping points (overplotting).
geom_smooth(method = "lm", ...): This is the key layer
for our analysis.
method = "lm" tells ggplot to fit a linear model.
color = "red" makes the regression line red.
se = TRUE adds a shaded 95% confidence interval around
the regression line, representing the uncertainty in the line’s
position.
labs(): Adds custom, informative labels for the title,
subtitle, and axes.
theme_minimal(): Applies a clean, minimal theme to the
plot for better readability.
This analysis successfully demonstrated a complete workflow:
We used set.seed for reproducibility.
We simulated a dataset where y has a known linear
relationship with x.
We confirmed this relationship statistically using the
lm() function, which recovered estimates very close to the
true parameters we set.
We effectively visualized the positive linear relationship and the model fit using a scatter plot with a regression line from ggplot2.