Econometrics_Notes

http://shujuren.org/article/42.html

 

1 intro

> install.packages("plm")
also installing the dependencies ‘miscTools’, ‘Formula’, ‘bdsmatrix’, ‘zoo’, ‘sandwich’, ‘lmtest’, ‘maxLik’

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/miscTools_0.6-22.zip'
Content type 'application/zip' length 63121 bytes (61 KB)
downloaded 61 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/Formula_1.2-2.zip'
Content type 'application/zip' length 164600 bytes (160 KB)
downloaded 160 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/bdsmatrix_1.3-3.zip'
Content type 'application/zip' length 183101 bytes (178 KB)
downloaded 178 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/zoo_1.8-1.zip'
Content type 'application/zip' length 915755 bytes (894 KB)
downloaded 894 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/sandwich_2.4-0.zip'
Content type 'application/zip' length 1243100 bytes (1.2 MB)
downloaded 1.2 MB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/lmtest_0.9-35.zip'
Content type 'application/zip' length 289030 bytes (282 KB)
downloaded 282 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/maxLik_1.3-4.zip'
Content type 'application/zip' length 312374 bytes (305 KB)
downloaded 305 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/plm_1.6-6.zip'
Content type 'application/zip' length 1594909 bytes (1.5 MB)
downloaded 1.5 MB

package ‘miscTools’ successfully unpacked and MD5 sums checked
package ‘Formula’ successfully unpacked and MD5 sums checked
package ‘bdsmatrix’ successfully unpacked and MD5 sums checked
package ‘zoo’ successfully unpacked and MD5 sums checked
package ‘sandwich’ successfully unpacked and MD5 sums checked
package ‘lmtest’ successfully unpacked and MD5 sums checked
package ‘maxLik’ successfully unpacked and MD5 sums checked
package ‘plm’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
	C:\Users\lenovo\AppData\Local\Temp\RtmpKSm1zE\downloaded_packages
> library(plm)
载入需要的程辑包:Formula

 

> #redefining variables
> Y <- cbind(mpg)
> X <- cbind(weight, length, foreign)
> summary(Y)
      mpg       
 Min.   :14.00  
 1st Qu.:17.25  
 Median :21.00  
 Mean   :20.92  
 3rd Qu.:23.00  
 Max.   :35.00  
> summary(X)
     weight         length         foreign      
 Min.   :2020   Min.   :163.0   Min.   :0.0000  
 1st Qu.:2642   1st Qu.:173.2   1st Qu.:0.0000  
 Median :3200   Median :191.0   Median :0.0000  
 Mean   :3099   Mean   :190.1   Mean   :0.2692  
 3rd Qu.:3610   3rd Qu.:203.0   3rd Qu.:0.7500  
 Max.   :4330   Max.   :222.0   Max.   :1.0000  
> olsreg <- lm(Y ~ X)
> summary(olsreg)

Call:
lm(formula = Y ~ X)

Residuals:
    Min      1Q  Median      3Q     Max 
-4.3902 -1.2734 -0.2991  0.7241  8.5203 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept) 44.968582   9.322678   4.824 8.08e-05 ***
Xweight     -0.005008   0.002188  -2.289    0.032 *  
Xlength     -0.043056   0.076926  -0.560    0.581    
Xforeign    -1.269211   1.632134  -0.778    0.445    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 2.917 on 22 degrees of freedom
Multiple R-squared:  0.6693,	Adjusted R-squared:  0.6242 
F-statistic: 14.84 on 3 and 22 DF,  p-value: 1.673e-05

  

2.linear regression in r

https://feliperego.github.io/blog/2015/10/23/Interpreting-Model-Output-In-R 

 

> mydata = read.csv(file.choose())
> View(mydata)
> attach(mydata)
> 
> Y = cbind(mpg)
> X1 = cbind(weight1)
> X = cbind(weight1, price, foreign)
> ?cbind
> summary(Y)
      mpg       
 Min.   :14.00  
 1st Qu.:17.25  
 Median :21.00  
 Mean   :20.92  
 3rd Qu.:23.00  
 Max.   :35.00  
> summary(X)
    weight1          price          foreign      
 Min.   :2.020   Min.   : 3299   Min.   :0.0000  
 1st Qu.:2.643   1st Qu.: 4466   1st Qu.:0.0000  
 Median :3.200   Median : 5146   Median :0.0000  
 Mean   :3.099   Mean   : 6652   Mean   :0.2692  
 3rd Qu.:3.610   3rd Qu.: 8054   3rd Qu.:0.7500  
 Max.   :4.330   Max.   :15906   Max.   :1.0000  
> 
> olsreg1 = lm(Y ~ X1)
> summary(olsreg1)

Call:
lm(formula = Y ~ X1)

Residuals:
    Min      1Q  Median      3Q     Max 
-5.4123 -1.6073 -0.1043  0.9261  8.1072 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  38.0665     2.6112  14.578 2.02e-13 ***
X1           -5.5315     0.8229  -6.722 5.93e-07 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 2.86 on 24 degrees of freedom
Multiple R-squared:  0.6531,	Adjusted R-squared:  0.6387 
F-statistic: 45.19 on 1 and 24 DF,  p-value: 5.935e-07

> confint(olsreg1)
                2.5 %    97.5 %
(Intercept) 32.677256 43.455664
X1          -7.229797 -3.833196
> anova(olsreg1)
Analysis of Variance Table

Response: Y
          Df Sum Sq Mean Sq F value    Pr(>F)    
X1         1 369.57  369.57  45.189 5.935e-07 ***
Residuals 24 196.28    8.18                      
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> plot(Y ~ X1, data = mydata)
> abline(olsreg1)
> 
> #predict values for dependent variables
> Y1hat = fitted(olsreg1)
> summary(Y1hat)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  14.12   18.10   20.37   20.92   23.45   26.89 
> plot(Y1hat ~ X1)
> e1hat = resid(olsreg1)
> summary(e1hat)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
-5.4123 -1.6073 -0.1043  0.0000  0.9261  8.1072 
> plot(elhat ~ X1)
Error in eval(predvars, data, env) : object 'elhat' not found
> plot(e1hat ~ X1)
> 
> olsreg2 = lm(Y ~ X)
> summary(olsreg2)

Call:
lm(formula = Y ~ X)

Residuals:
    Min      1Q  Median      3Q     Max 
-4.6942 -1.1857 -0.0452  0.6433  8.6895 

Coefficients:
              Estimate Std. Error t value Pr(>|t|)    
(Intercept) 42.1661962  4.2647533   9.887 1.48e-09 ***
Xweight1    -7.1211114  1.6046735  -4.438 0.000207 ***
Xprice       0.0002258  0.0002654   0.851 0.404002    
Xforeign    -2.5071265  2.0565685  -1.219 0.235723    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 2.89 on 22 degrees of freedom
Multiple R-squared:  0.6752,	Adjusted R-squared:  0.6309 
F-statistic: 15.25 on 3 and 22 DF,  p-value: 1.374e-05

> confint(olsreg2)
                    2.5 %        97.5 %
(Intercept)  3.332164e+01 51.0107531780
Xweight1    -1.044900e+01 -3.7932221856
Xprice      -3.245229e-04  0.0007760878
Xforeign    -6.772188e+00  1.7579354345
> anova(olsreg2)
Analysis of Variance Table

Response: Y
          Df Sum Sq Mean Sq F value    Pr(>F)    
X          3 382.08 127.360  15.247 1.374e-05 ***
Residuals 22 183.77   8.353                      
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> Yhat = fitted(olsreg2)
> summary(Yhat)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  13.90   17.91   20.46   20.92   23.99   27.89 
> ehat = resid(olsreg2)
> summary(ehat)
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
-4.69416 -1.18567 -0.04524  0.00000  0.64332  8.68946 

  3. panel data models in r

http://eclr.humanities.manchester.ac.uk/index.php/Panel_in_R 

# Panel Data Models in R
# Copyright 2013 by Ani Katchova

# install.packages("plm")
library(plm)

mydata<- read.csv("C:/Econometrics/Data/panel_wage.csv")
attach(mydata)

Y <- cbind(lwage)
X <- cbind(exp, exp2, wks, ed)

# Set data as panel data
pdata <- plm.data(mydata, index=c("id","t"))

# Descriptive statistics
summary(Y)
summary(X)

# Pooled OLS estimator
pooling <- plm(Y ~ X, data=pdata, model= "pooling")
summary(pooling)

# Between estimator
between <- plm(Y ~ X, data=pdata, model= "between")
summary(between)

# First differences estimator
firstdiff <- plm(Y ~ X, data=pdata, model= "fd")
summary(firstdiff)

# Fixed effects or within estimator
fixed <- plm(Y ~ X, data=pdata, model= "within")
summary(fixed)

# Random effects estimator
random <- plm(Y ~ X, data=pdata, model= "random")
summary(random)

# LM test for random effects versus OLS
plmtest(pooling)

# LM test for fixed effects versus OLS
pFtest(fixed, pooling)

# Hausman test for fixed versus random effects model
phtest(random, fixed)

  

  

posted @ 2018-03-09 16:34  howlowl  阅读(504)  评论(0编辑  收藏  举报