Created by Vasianina Arina, Titov Vladislav, Popov Nick, students of BEC 192

library(readxl)#excel
library(writexl)

library(dplyr)
library(tidyverse)
library(plm)
library(sandwich)
library(clubSandwich)
library(lmtest)
library(psych)
library(ivreg)

library(stargazer)
data <- read_xlsx('C:/Users/Kolya/Documents/Python Scripts/EIO/cars1_Data_with_IV.xlsx')
## New names:
## * `` -> ...1
data

(г)

reg_ols_1 <- lm(MR ~ horsepower + fuel + width + height + weight + domestic
               + factor(year) + factor(country) + factor(co), data = data)

cov_ols_1 <- vcovHC(reg_ols_1, type = "HC0")
se_ols_1 <- sqrt(diag(cov_ols_1))

#coeftest(reg_ols_1, df = Inf, vcov = cov_ols_1)
stargazer(keep = c('horsepower', 'fuel', 'width', 'height', 'weight', 'domestic'), reg_ols_1, se = list(se_ols_1), type = "text", keep.stat = c("n"), font.size = "tiny")
## 
## ========================================
##                  Dependent variable:    
##              ---------------------------
##                          MR             
## ----------------------------------------
## horsepower            0.004***          
##                       (0.0001)          
##                                         
## fuel                   -0.002           
##                        (0.001)          
##                                         
## width                 0.002***          
##                       (0.0004)          
##                                         
## height                 0.0005           
##                       (0.0004)          
##                                         
## weight                0.0002***         
##                       (0.00002)         
##                                         
## domestic              -0.009***         
##                        (0.002)          
##                                         
## ----------------------------------------
## Observations           11,483           
## ========================================
## Note:        *p<0.1; **p<0.05; ***p<0.01

The following coefficients have significant influence on MC: horsepower, width, weight, domestic. So, horsepower and width, weight have a positive effect on MC (so, the more horsepower and wight, weight are, the more costly it is to produce this car), while domestic has a negative impact(its cheaper to produce domestic car than import some technologies from abroad).

д)

reg_tsls_1 <- ivreg(MR ~ horsepower + fuel + width + height + weight + domestic + qu
               + factor(year) + factor(country) + factor(co) 
                    | horsepower + fuel + width + height + weight + domestic +  factor(year)
                    + factor(country) + factor(co) + demand_instruments0
                    + demand_instruments1 + demand_instruments2 + demand_instruments3 + demand_instruments4
                    + demand_instruments5 + demand_instruments6 + demand_instruments7 + demand_instruments8 
                    + demand_instruments9 + demand_instruments10 + demand_instruments11,
                    data = data)

cov_tsls_1 <- vcovHC(reg_tsls_1, type = "HC0")
se_tsls_1 <- sqrt(diag(cov_tsls_1))

#coeftest(reg_tsls_1, df = Inf, vcov = cov_tsls_1)
stargazer(keep = c('qu', 'horsepower', 'fuel', 'width', 'height', 'weight', 'domestic'), reg_ols_1, reg_tsls_1, se = list(se_ols_1, se_tsls_1), type = "text", keep.stat = c("n"), font.size = "tiny")
## 
## =========================================
##                  Dependent variable:     
##              ----------------------------
##                           MR             
##                   OLS       instrumental 
##                               variable   
##                   (1)            (2)     
## -----------------------------------------
## horsepower      0.004***      0.004***   
##                 (0.0001)      (0.0002)   
##                                          
## fuel             -0.002       -0.004***  
##                 (0.001)        (0.001)   
##                                          
## width           0.002***      0.003***   
##                 (0.0004)       (0.001)   
##                                          
## height           0.0005        0.001**   
##                 (0.0004)      (0.0005)   
##                                          
## weight         0.0002***      0.0002***  
##                (0.00002)      (0.00002)  
##                                          
## domestic       -0.009***       0.066**   
##                 (0.002)        (0.027)   
##                                          
## qu                           -0.00000*** 
##                               (0.00000)  
##                                          
## -----------------------------------------
## Observations     11,483        11,483    
## =========================================
## Note:         *p<0.1; **p<0.05; ***p<0.01

Well, every coefficient is now significant (fuel and height were not before). Also domestic has changed the sign. Using IVs was useful. However, the problem, of weak or excessive IVs could take place, because IVs were made not based on economic judgement, but mechanically. Quantity has a negative sign- so, we have an increasing return to scale (the more we produce, the cheaper it costs us).

е)

#we took ln, so, lets make a change not for 5000 units (we have a model log-lin)
ceofs = coeftest(reg_tsls_1, df = Inf, vcov = cov_tsls_1)
change = ceofs[8, 1]*100
change
## [1] -0.0001335073

If we change the qu for 5000, MR will change for -0.0001%.