Discussion_1

#Packages list
library(AER)
Loading required package: car
Loading required package: carData
Loading required package: lmtest
Loading required package: zoo

Attaching package: 'zoo'
The following objects are masked from 'package:base':

    as.Date, as.Date.numeric
Loading required package: sandwich
Loading required package: survival
library(psych)

Attaching package: 'psych'
The following object is masked from 'package:car':

    logit

I.

1.

data(package = "AER")
data1 <- data(CPSSW9298)
data2 <- data(CigarettesSW)

2.

Data set 1

This data set contains information from the Current Population Survey, showing demographic characteristics of different individuals across two different years, 1992 and 1998. The samples from each year seem to represent different individuals, rather than tracking the same individuals over time.

The characteristics of the population include earnings (income), gender, age, and degree (education level)

Data set 2

This data set contains information about cigarette consumption in the United States, showing certain variables of different states across two different years, 1985 and 1995. The data set follows the same 48 states over those two periods of time.

The variables are consumer price index, population, packs per capita, state personal income, tax (average state,federal,and average local excise taxes for fiscal year), price, and taxs (average excise taxes for fiscal year, including sales tax).

3.

Data set 1

summary(CPSSW9298)
   year         earnings             degree        gender          age       
 1992:7590   Min.   : 1.840   highschool:7936   male  :7815   Min.   :25.00  
 1998:5911   1st Qu.: 8.173   bachelor  :5565   female:5686   1st Qu.:27.00  
             Median :11.538                                   Median :30.00  
             Mean   :12.663                                   Mean   :29.71  
             3rd Qu.:15.865                                   3rd Qu.:32.00  
             Max.   :49.451                                   Max.   :34.00  
head(CPSSW9298)
  year  earnings     degree gender age
1 1992 11.188811   bachelor   male  29
2 1992 10.000000   bachelor   male  33
3 1992  5.769231 highschool   male  30
4 1992 14.957265   bachelor   male  31
5 1992  8.660096   bachelor female  26
6 1992  7.788462 highschool female  31
tail(CPSSW9298)
      year  earnings     degree gender age
13496 1998  4.738461 highschool female  27
13497 1998  6.730769 highschool female  29
13498 1998 12.019231 highschool female  32
13499 1998 11.111111   bachelor   male  28
13500 1998 16.826923   bachelor female  33
13501 1998 12.019231   bachelor female  34

This data set is a pooled-cross sectional data.

Data set 2

summary(CigarettesSW)
     state      year         cpi          population           packs       
 AL     : 2   1985:48   Min.   :1.076   Min.   :  478447   Min.   : 49.27  
 AR     : 2   1995:48   1st Qu.:1.076   1st Qu.: 1622606   1st Qu.: 92.45  
 AZ     : 2             Median :1.300   Median : 3697472   Median :110.16  
 CA     : 2             Mean   :1.300   Mean   : 5168866   Mean   :109.18  
 CO     : 2             3rd Qu.:1.524   3rd Qu.: 5901500   3rd Qu.:123.52  
 CT     : 2             Max.   :1.524   Max.   :31493524   Max.   :197.99  
 (Other):84                                                                
     income               tax            price             taxs       
 Min.   :  6887097   Min.   :18.00   Min.   : 84.97   Min.   : 21.27  
 1st Qu.: 25520384   1st Qu.:31.00   1st Qu.:102.71   1st Qu.: 34.77  
 Median : 61661644   Median :37.00   Median :137.72   Median : 41.05  
 Mean   : 99878736   Mean   :42.68   Mean   :143.45   Mean   : 48.33  
 3rd Qu.:127313964   3rd Qu.:50.88   3rd Qu.:176.15   3rd Qu.: 59.48  
 Max.   :771470144   Max.   :99.00   Max.   :240.85   Max.   :112.63  
                                                                      
table(CigarettesSW$state)

AL AR AZ CA CO CT DE FL GA IA ID IL IN KS KY LA MA MD ME MI MN MO MS MT NC ND 
 2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2 
NE NH NJ NM NV NY OH OK OR PA RI SC SD TN TX UT VA VT WA WI WV WY 
 2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2 

This data set is panel data

II.

  • Covariance is an indicator of the extend to which two variables are dependent on each other. The value of covariance lies between \(-\infty\) and \(+\infty\) . If it is positive, the two variables move in the same direction. If the correlation is 0, there is no relationship between the variables, and if it’s negative, the variables move in different directions.

  • Variance is a measure of dispersion. It measures how spread out points are with respect to their average value.

  • Dividing the covariance of \(cov(x,y)\) by the variance of \(var(x)\) gives us the slope because we are normalizing the relationship of \(x\) and \(y\) . It quantifies the change in \(y\) by a unit change in \(x\).

OLS output

\[ Packs_i = \beta_0 + \beta_1Taxs_i + \epsilon_i \]

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")

Covariance-variance output

beta1 <- cov(data1$packs, data1$taxs)/var(data1$taxs)
round(beta1, digits = 4)
[1] -1.2643
beta0 <-  mean(data1$packs) - beta1 * mean(data1$taxs)
beta0  
[1] 166.0152

Bonus

\[ \beta_1 = \frac {\sum y_i x_i - n \sum y_i \sum x_i} {\sum x_i^2 - n(\sum x_i)^2} \]

\[ \beta_1 = \frac {\sum y_i x_i - \bar{y_i} \sum x_i} {\sum x_i^2 - \bar{x_i} \sum x_i} \]