library(AER)
## Warning: package 'AER' was built under R version 4.3.3
## Loading required package: car
## Loading required package: carData
## Loading required package: lmtest
## Warning: package 'lmtest' was built under R version 4.3.3
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.3.3
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: sandwich
## Warning: package 'sandwich' was built under R version 4.3.3
## Loading required package: survival
data1 <- data(CPSSW9298)
data2 <- data(CigarettesSW)
The "CPSSW9298" dataset from the AER package contains information about the Consumer Price Index (CPI) survey conducted by the United States Bureau of Labor Statistics. It includes data on various demographic and economic characteristics of households, such as income, household composition, and region. The dataset allows researchers to analyze the relationship between these variables and consumer price inflation.
The "CigarettesSW" dataset from the AER package provides data on cigarette consumption in the United States. It includes variables such as the average price of cigarettes, average income, and measures of anti-smoking policies across different states and years. This dataset enables researchers to examine the impact of factors like price and policy on cigarette consumption patterns.
data("Affairs", package = "AER")
head(Affairs)
## affairs gender age yearsmarried children religiousness education occupation
## 4 0 male 37 10.00 no 3 18 7
## 5 0 female 27 4.00 no 4 14 6
## 11 0 female 32 15.00 yes 1 12 1
## 16 0 male 57 15.00 yes 5 18 6
## 23 0 male 22 0.75 no 2 17 6
## 29 0 female 32 1.50 no 2 17 5
## rating
## 4 4
## 5 4
## 11 4
## 16 5
## 23 3
## 29 5
summary(Affairs)
## affairs gender age yearsmarried children
## Min. : 0.000 female:315 Min. :17.50 Min. : 0.125 no :171
## 1st Qu.: 0.000 male :286 1st Qu.:27.00 1st Qu.: 4.000 yes:430
## Median : 0.000 Median :32.00 Median : 7.000
## Mean : 1.456 Mean :32.49 Mean : 8.178
## 3rd Qu.: 0.000 3rd Qu.:37.00 3rd Qu.:15.000
## Max. :12.000 Max. :57.00 Max. :15.000
## religiousness education occupation rating
## Min. :1.000 Min. : 9.00 Min. :1.000 Min. :1.000
## 1st Qu.:2.000 1st Qu.:14.00 1st Qu.:3.000 1st Qu.:3.000
## Median :3.000 Median :16.00 Median :5.000 Median :4.000
## Mean :3.116 Mean :16.17 Mean :4.195 Mean :3.932
## 3rd Qu.:4.000 3rd Qu.:18.00 3rd Qu.:6.000 3rd Qu.:5.000
## Max. :5.000 Max. :20.00 Max. :7.000 Max. :5.000
tail(Affairs)
## affairs gender age yearsmarried children religiousness education
## 1935 7 male 47 15.0 yes 3 16
## 1938 1 male 22 1.5 yes 1 12
## 1941 7 female 32 10.0 yes 2 18
## 1954 2 male 32 10.0 yes 2 17
## 1959 2 male 22 7.0 yes 3 18
## 9010 1 female 32 15.0 yes 3 14
## occupation rating
## 1935 4 2
## 1938 2 5
## 1941 5 4
## 1954 6 5
## 1959 6 2
## 9010 1 5
data("mtcars")
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
summary(mtcars)
## mpg cyl disp hp
## Min. :10.40 Min. :4.000 Min. : 71.1 Min. : 52.0
## 1st Qu.:15.43 1st Qu.:4.000 1st Qu.:120.8 1st Qu.: 96.5
## Median :19.20 Median :6.000 Median :196.3 Median :123.0
## Mean :20.09 Mean :6.188 Mean :230.7 Mean :146.7
## 3rd Qu.:22.80 3rd Qu.:8.000 3rd Qu.:326.0 3rd Qu.:180.0
## Max. :33.90 Max. :8.000 Max. :472.0 Max. :335.0
## drat wt qsec vs
## Min. :2.760 Min. :1.513 Min. :14.50 Min. :0.0000
## 1st Qu.:3.080 1st Qu.:2.581 1st Qu.:16.89 1st Qu.:0.0000
## Median :3.695 Median :3.325 Median :17.71 Median :0.0000
## Mean :3.597 Mean :3.217 Mean :17.85 Mean :0.4375
## 3rd Qu.:3.920 3rd Qu.:3.610 3rd Qu.:18.90 3rd Qu.:1.0000
## Max. :4.930 Max. :5.424 Max. :22.90 Max. :1.0000
## am gear carb
## Min. :0.0000 Min. :3.000 Min. :1.000
## 1st Qu.:0.0000 1st Qu.:3.000 1st Qu.:2.000
## Median :0.0000 Median :4.000 Median :2.000
## Mean :0.4062 Mean :3.688 Mean :2.812
## 3rd Qu.:1.0000 3rd Qu.:4.000 3rd Qu.:4.000
## Max. :1.0000 Max. :5.000 Max. :8.000
tail(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.7 0 1 5 2
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.9 1 1 5 2
## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.5 0 1 5 4
## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.5 0 1 5 6
## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.6 0 1 5 8
## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.6 1 1 4 2
The "CPSSW9298" dataset can be classified as panel data. It consists of repeated observations on households over time, allowing for the analysis of both cross-sectional and time series variations. The panel structure enables researchers to capture individual-level effects, time trends, and the interaction between the two.
The "CigarettesSW" dataset is also panel data as it contains observations on cigarette consumption across different states and years. This dataset allows for the analysis of both cross-sectional and time series variations, enabling researchers to examine the effects of policy changes and other factors on cigarette consumption patterns over time and between states.
mtcars Description: The mtcars dataset comprises 32 observations on 11 variables, detailing aspects like miles per gallon (MPG), number of cylinders, horsepower, and weight for various car models. This dataset provides insights into how different design choices impact vehicle performance.
Affairs Description: The Affairs dataset includes information on extramarital affairs, rating of marriage, age, years married, and other demographic factors for a sample of individuals. It offers a glimpse into personal and social factors associated with marital satisfaction and infidelity.
In a simple linear regression, the slope coefficient (β1) represents the change in the dependent variable (y) for each unit change in the independent variable (x). It can be calculated as the covariance between y and x divided by the variance of x. The covariance between two variables, y and x, measures how they vary together. It indicates the extent to which changes in y correspond to changes in x. The variance of x measures the spread or variability of x around its mean. The formula for the slope coefficient (β1) is: β1 = Cov(y, x) / Var(x) Where: Cov(y, x) represents the covariance between y and x. Var(x) represents the variance of x. By dividing the covariance by the variance, we normalize the relationship between the two variables and obtain the slope coefficient.
# Scatterplot of mpg versus wt
plot(mtcars$wt, mtcars$mpg, xlab = "Weight", ylab = "Miles per Gallon", main = "Scatterplot: mpg vs. wt")
# Simple linear regression
lm_model <- lm(mpg ~ wt, data = mtcars)
summary(lm_model) # Output of the regression
##
## Call:
## lm(formula = mpg ~ wt, data = mtcars)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.5432 -2.3647 -0.1252 1.4096 6.8727
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 37.2851 1.8776 19.858 < 2e-16 ***
## wt -5.3445 0.5591 -9.559 1.29e-10 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.046 on 30 degrees of freedom
## Multiple R-squared: 0.7528, Adjusted R-squared: 0.7446
## F-statistic: 91.38 on 1 and 30 DF, p-value: 1.294e-10
# Calculate slope using formula
covariance <- cov(mtcars$mpg, mtcars$wt)
variance <- var(mtcars$wt)
slope_formula <- covariance / variance
slope_formula # Output of the formula
## [1] -5.344472
data1 <- subset(CigarettesSW, year == 1985)
#created a new cross-sectional data set of observations only in 1985
lm1 <- lm(data= data1,
formula = packs ~ taxs)
summary(lm1)
##
## Call:
## lm(formula = packs ~ taxs, data = data1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -54.683 -7.618 -1.726 8.589 73.700
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 166.0152 16.3061 10.18 2.29e-13 ***
## taxs -1.2643 0.4615 -2.74 0.00872 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 19.88 on 46 degrees of freedom
## Multiple R-squared: 0.1403, Adjusted R-squared: 0.1216
## F-statistic: 7.507 on 1 and 46 DF, p-value: 0.00872
plot(x = data1$taxs,
y = data1$packs,
xlim = c(0,60),
xlab = "Average excise taxes for fiscal year, including sales tax",
ylab = "Packs of cigarettes per capita",
main = "Cigarette consumption in the US in 1985")
abline(reg = lm1,
lwd = 2,
col = "red")
beta1 <- cov(data1$packs, data1$taxs)/var(data1$taxs)
round(beta1, digits = 4)
## [1] -1.2643
beta1 <- cov(data1$packs, data1$taxs)/var(data1$taxs)
round(beta1, digits = 4)
## [1] -1.2643
Specify the linear regression model: y = β0 + β1x + ε Where y is the dependent variable, x is the independent variable, β0 and β1 are the regression coefficients, and ε is the error term. Define the sum of squared residuals (SSR): SSR = Σ(yᵢ - (β0 + β1xᵢ))² Where Σ represents the summation over all observations (i = 1 to n). Minimize the SSR with respect to β0 and β1: To find the optimal values of β0 and β1, take the parial derivatives of SSR with respect to β0 and β1, set them equal to zero, and solve the resulting equations. ∂SSR/∂β0 = -2Σ(yᵢ - β0 - β1xᵢ) = 0 ∂SSR/∂β1 = -2Σxᵢ(yᵢ - β0 - β1xᵢ) = 0 Solve the normal equations: Solving the above equations simultaneously gives the estimates for β0 and β1: Σyᵢ - nβ0 - β1Σxᵢ = 0 Σxᵢyᵢ - β0Σxᵢ - β1Σxᵢ² = 0 Rearrange the equations to solve for β0 and β1: β0 = (Σyᵢ - β1Σxᵢ) / n β1 = (Σxᵢyᵢ - β0Σxᵢ) / Σxᵢ² Calculate the estimates: Substitute the values of β0 and β1 back into the regression equation to obtain the estimated values: ŷ = β̂0 + β̂1*x Where ŷ is the predicted value of y, β̂0 is the estimated intercept, and β̂1 is the estimated slope.