Simple Linear Regression Notes
Introduction to Regression Analysis
Definition: Regression analysis predicts a dependent variable (response/outcome) using one or more independent variables (predictor/explanatory).
Types based on:
Number of independent variables (simple vs. multiple)
Relationship (linear vs. nonlinear)
Focus: Simple linear regression - analysis with one independent variable.
Simple Linear Regression Model
Equation: Y = α + βX + ϵ
Y: Dependent variable (response)
X: Independent variable (predictor)
α: Intercept
β: Coefficient (slope)
ϵ: Error term
Goal: Estimate α and β that best describe the relationship between X and Y.
Visualizing Simple Linear Regression
Data Plotting: Start by graphing the data points (X, Y) to spot any linear trend.
Fitting a Line: Multiple methods exist for fitting lines; the objective is to determine the line that best captures this trend using regression analysis.
The Mechanism of Least Squares
Distance Measurement: Calculate vertical distances from the fitted line to each data point:
For data point (x1, y1): distance = a - y1
Sum of Squared Residuals (SSR):
SSR = Σ(a - yi)² (negative values complicate analysis)
Redefine to minimize SSR, leading to the least squares approach.
Goal: Find (α, β) to minimize SSR:
SSR = Σ[(α + βxi) - yi]²
Application in Business Context
Risk Analysis Case Study: Analyze investment risk for Tesla, Inc. through its financial beta calculation.
Beta Definition: Measure of stock volatility relative to the market.
Beta > 1: Higher risk/volatility
Beta < 1: Lower risk/volatility
Practical Task: Calculate Tesla's beta to inform investment strategies based on portfolio risk tolerance.
Data Preparation for Beta Calculation
Regression Model to estimate Tesla’s beta:
RetTSLA,t = α + β Retmkt,t + ϵ
Variables:
RetTSLA,t: Tesla's monthly returns
Retmkt,t: Market returns (S&P 500 index)
Data Source: Extract from Yahoo Finance for selected months.
R Coding for Analysis
Loading Data:
library(tidyverse)
df1 <- read.csv("TSLA_mly_2010_2013.csv")
df1 %>% glimpse()
Variables in Dataset:
date, volTSLA, adjCloseTSLA, volSP500, adjCloseSP500
Focus on Stock Prices:
df1 <- df1 %>% select(date, adjCloseTSLA, adjCloseSP500)
Calculating Monthly Returns:
df1 <- df1 %>% na.omit() %>% mutate(
lagTSLA = lag(adjCloseTSLA),
lagSP = lag(adjCloseSP500),
rtrTSLA = (adjCloseTSLA - lagTSLA) / lagTSLA,
rtrSP500 = (adjCloseSP500 - lagSP) / lagSP
)
Visualization of Returns
Scatter Plot: Visualize relationship between Tesla’s and S&P 500 monthly returns.
Could be Created with:
ggplot(df1, aes(x = rtrSP500, y = rtrTSLA)) + geom_point()
Performing the Regression in R
Using lm() Function:
betaTSLA <- lm(rtrTSLA ~ rtrSP500, df1)
summary(betaTSLA)
Interpreting Output: The summary gives information on intercept (α) and slope (β):
Example Equation: rtrTSLA = 0.06931 + 0.69077 * rtrSP500
Tutorial Seminar Objectives
Replicate the regression analysis learned to deepen understanding.
Create scatter plots using the
ggplot2package in R.Estimate betas for other stocks (Coca-Cola, Johnson & Johnson).
ggplot2 for Visualization
Scatter Plot Example:
ggplot(df1, aes(x = rtrSP500, y = rtrTSLA)) +
geom_point(color = "steelblue", alpha = 0.7, size = 1.5) +
geom_smooth(method = "lm", se = FALSE, color = "orangered") +
labs(title = "Tesla and S&P 500 Monthly Returns", x = "S&P 500 Monthly Returns", y = "Tesla Monthly Returns") +
theme_bw()
Options for Customization: Color, size, and transparency can be adjusted to improve interpretability of the plot.
Conclusion
Key Takeaway: Encouragement to practically apply regression analysis using R to inform business investment decisions based on risk assessment and visualization techniques.