Veri tabanının oluşturulması

  • Paketlerin yüklenmesi
library(data.table)
library(ggplot2)
library(mice)
## 
## Attaching package: 'mice'
## The following object is masked from 'package:stats':
## 
##     filter
## The following objects are masked from 'package:base':
## 
##     cbind, rbind
library(plm)
## 
## Attaching package: 'plm'
## The following object is masked from 'package:data.table':
## 
##     between
library(stargazer)
## 
## Please cite as:
##  Hlavac, Marek (2018). stargazer: Well-Formatted Regression and Summary Statistics Tables.
##  R package version 5.2.2. https://CRAN.R-project.org/package=stargazer
  • Firma veri seti hazırlanması
nf <- 1000
nt <- 20
ns <- 10
nobs <- nf*nt

kelas <- 0.1
lelas <- 0.2
melas <- 0.75

id <- rep(c(1:nf), each = nt)
year <- rep(c(1:nt), times = nf)
sect <- rep(c(1:ns), each = nobs / ns)
kg <- rnorm(nobs, 0.01, 0.02)
lg <- (kg/10) + rnorm(nobs, 0.01, 0.01)
mg <- (lg/10) + rnorm(nobs, 0.005, 0.01)
tc <- rnorm(nobs, 0.01, 0.01)
fs <- max(0.1, rep(rnorm(nf, 2, 0.5), each = nt))
re <- max(0.1, rep(rnorm(nf, 0, 0.5), each = nt))
ee <- rnorm(nobs, 0, 0.1)

fdata <- data.table(id, year, sect, kg, lg, mg, tc, fs, re, ee)
fdata[, lk := fs*(1 + cumsum(kg)), by = id]
fdata[, ll := fs*(1 + cumsum(lg)), by = id]
fdata[, lm := fs*(1 + cumsum(mg)), by = id]
fdata[, lA := fs*(1 + cumsum(tc)), by = id]
fdata[, fe := max(0.1, rep(rnorm(nf, 0, 0.5)) + mean(lk)/5 + mean(ll)/5), by = id]
fdata[, lq := (lA + kelas*lk + lelas*ll + melas*lm) + ee]
fdata[, lqre := re + (lA + kelas*lk + lelas*ll + melas*lm) + ee]
fdata[, lqfe := fe + (lA + kelas*lk + lelas*ll + melas*lm) + ee]
  • Bazı kayıp değerler yaratalım
  • % 2.5 oranında tamamen rastsal kayıp değerler
fdata[0.025 > runif(nf*nt), lk := NA]
fdata[0.025 > runif(nf*nt), ll := NA]
fdata[0.025 > runif(nf*nt), lm := NA]
  • Firmaların %2.5’i için kayıp değerler yaratalım
fdata[id %in% sample(1:nf, floor(0.025*nf)), lk := NA]
fdata[id %in% sample(1:nf, floor(0.025*nf)), ll := NA]
fdata[id %in% sample(1:nf, floor(0.025*nf)), lm := NA]

Veri setini kontrolü

head(fdata)
##    id year sect           kg          lg           mg           tc       fs
## 1:  1    1    1  0.003436922 0.018036581  0.015180031 -0.011610822 3.664568
## 2:  1    2    1  0.052387616 0.018382570 -0.009806749  0.021432255 3.664568
## 3:  1    3    1  0.020034352 0.001411454  0.002170987  0.011186517 3.664568
## 4:  1    4    1  0.021445091 0.019488009  0.005137284  0.007264164 3.664568
## 5:  1    5    1 -0.009941275 0.015467835  0.014920020  0.012481722 3.664568
## 6:  1    6    1 -0.001466867 0.018948229  0.014751797  0.005541357 3.664568
##          re           ee       lk       ll       lm       lA       fe       lq
## 1: 1.759557  0.188908390 3.677162 3.730664 3.720196 3.622019 3.137542 7.714923
## 2: 1.759557 -0.052586450 3.869140 3.798028 3.684258 3.700559 3.137542 7.557686
## 3: 1.759557 -0.009281759 3.942558 3.803200 3.692214 3.741553 3.137542 7.656327
## 4: 1.759557  0.267214680 4.021145       NA 3.711040 3.768173 3.137542 7.995705
## 5: 1.759557 -0.059906271 3.984714 3.931298 3.765715 3.813913 3.137542 7.763024
## 6: 1.759557 -0.030455901 3.979339 4.000735 3.819774 3.834219 3.137542 7.866675
##        lqre     lqfe
## 1: 9.474480 10.85246
## 2: 9.317243 10.69523
## 3: 9.415884 10.79387
## 4: 9.755262 11.13325
## 5: 9.522581 10.90057
## 6: 9.626232 11.00422
str(fdata)
## Classes 'data.table' and 'data.frame':   20000 obs. of  18 variables:
##  $ id  : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ year: int  1 2 3 4 5 6 7 8 9 10 ...
##  $ sect: int  1 1 1 1 1 1 1 1 1 1 ...
##  $ kg  : num  0.00344 0.05239 0.02003 0.02145 -0.00994 ...
##  $ lg  : num  0.01804 0.01838 0.00141 0.01949 0.01547 ...
##  $ mg  : num  0.01518 -0.00981 0.00217 0.00514 0.01492 ...
##  $ tc  : num  -0.01161 0.02143 0.01119 0.00726 0.01248 ...
##  $ fs  : num  3.66 3.66 3.66 3.66 3.66 ...
##  $ re  : num  1.76 1.76 1.76 1.76 1.76 ...
##  $ ee  : num  0.18891 -0.05259 -0.00928 0.26721 -0.05991 ...
##  $ lk  : num  3.68 3.87 3.94 4.02 3.98 ...
##  $ ll  : num  3.73 3.8 3.8 NA 3.93 ...
##  $ lm  : num  3.72 3.68 3.69 3.71 3.77 ...
##  $ lA  : num  3.62 3.7 3.74 3.77 3.81 ...
##  $ fe  : num  3.14 3.14 3.14 3.14 3.14 ...
##  $ lq  : num  7.71 7.56 7.66 8 7.76 ...
##  $ lqre: num  9.47 9.32 9.42 9.76 9.52 ...
##  $ lqfe: num  10.9 10.7 10.8 11.1 10.9 ...
##  - attr(*, ".internal.selfref")=<externalptr> 
##  - attr(*, "index")= int(0) 
##   ..- attr(*, "__id")= int(0)
summary(fdata)
##        id              year            sect            kg           
##  Min.   :   1.0   Min.   : 1.00   Min.   : 1.0   Min.   :-0.061496  
##  1st Qu.: 250.8   1st Qu.: 5.75   1st Qu.: 3.0   1st Qu.:-0.003694  
##  Median : 500.5   Median :10.50   Median : 5.5   Median : 0.009913  
##  Mean   : 500.5   Mean   :10.50   Mean   : 5.5   Mean   : 0.009917  
##  3rd Qu.: 750.2   3rd Qu.:15.25   3rd Qu.: 8.0   3rd Qu.: 0.023340  
##  Max.   :1000.0   Max.   :20.00   Max.   :10.0   Max.   : 0.087958  
##                                                                     
##        lg                  mg                   tc                  fs       
##  Min.   :-0.027712   Min.   :-0.0365834   Min.   :-0.029034   Min.   :3.665  
##  1st Qu.: 0.003926   1st Qu.:-0.0006042   1st Qu.: 0.003148   1st Qu.:3.665  
##  Median : 0.011012   Median : 0.0060753   Median : 0.009989   Median :3.665  
##  Mean   : 0.010986   Mean   : 0.0060809   Mean   : 0.009968   Mean   :3.665  
##  3rd Qu.: 0.018015   3rd Qu.: 0.0127688   3rd Qu.: 0.016758   3rd Qu.:3.665  
##  Max.   : 0.054650   Max.   : 0.0438502   Max.   : 0.048748   Max.   :3.665  
##                                                                              
##        re             ee                  lk              ll       
##  Min.   :1.76   Min.   :-0.358443   Min.   :3.214   Min.   :3.481  
##  1st Qu.:1.76   1st Qu.:-0.065779   1st Qu.:3.802   1st Qu.:3.864  
##  Median :1.76   Median : 0.000957   Median :4.012   Median :4.070  
##  Mean   :1.76   Mean   : 0.001566   Mean   :4.050   Mean   :4.086  
##  3rd Qu.:1.76   3rd Qu.: 0.068347   3rd Qu.:4.250   3rd Qu.:4.282  
##  Max.   :1.76   Max.   : 0.381598   Max.   :5.557   Max.   :5.063  
##                                     NA's   :955     NA's   :988    
##        lm              lA              fe              lq       
##  Min.   :3.489   Min.   :3.562   Min.   :2.848   Min.   :7.242  
##  1st Qu.:3.760   1st Qu.:3.848   1st Qu.:3.122   1st Qu.:7.859  
##  Median :3.880   Median :4.034   Median :3.222   Median :8.184  
##  Mean   :3.899   Mean   :4.050   Mean   :3.242   Mean   :8.198  
##  3rd Qu.:4.016   3rd Qu.:4.227   3rd Qu.:3.345   3rd Qu.:8.511  
##  Max.   :4.671   Max.   :4.963   Max.   :3.893   Max.   :9.566  
##  NA's   :1018                                                   
##       lqre             lqfe      
##  Min.   : 9.001   Min.   :10.21  
##  1st Qu.: 9.619   1st Qu.:11.09  
##  Median : 9.944   Median :11.43  
##  Mean   : 9.958   Mean   :11.44  
##  3rd Qu.:10.271   3rd Qu.:11.76  
##  Max.   :11.325   Max.   :13.13  
## 
table(fdata$year, fdata$sect)
##     
##        1   2   3   4   5   6   7   8   9  10
##   1  100 100 100 100 100 100 100 100 100 100
##   2  100 100 100 100 100 100 100 100 100 100
##   3  100 100 100 100 100 100 100 100 100 100
##   4  100 100 100 100 100 100 100 100 100 100
##   5  100 100 100 100 100 100 100 100 100 100
##   6  100 100 100 100 100 100 100 100 100 100
##   7  100 100 100 100 100 100 100 100 100 100
##   8  100 100 100 100 100 100 100 100 100 100
##   9  100 100 100 100 100 100 100 100 100 100
##   10 100 100 100 100 100 100 100 100 100 100
##   11 100 100 100 100 100 100 100 100 100 100
##   12 100 100 100 100 100 100 100 100 100 100
##   13 100 100 100 100 100 100 100 100 100 100
##   14 100 100 100 100 100 100 100 100 100 100
##   15 100 100 100 100 100 100 100 100 100 100
##   16 100 100 100 100 100 100 100 100 100 100
##   17 100 100 100 100 100 100 100 100 100 100
##   18 100 100 100 100 100 100 100 100 100 100
##   19 100 100 100 100 100 100 100 100 100 100
##   20 100 100 100 100 100 100 100 100 100 100
table(fdata$year, fdata$sect)
##     
##        1   2   3   4   5   6   7   8   9  10
##   1  100 100 100 100 100 100 100 100 100 100
##   2  100 100 100 100 100 100 100 100 100 100
##   3  100 100 100 100 100 100 100 100 100 100
##   4  100 100 100 100 100 100 100 100 100 100
##   5  100 100 100 100 100 100 100 100 100 100
##   6  100 100 100 100 100 100 100 100 100 100
##   7  100 100 100 100 100 100 100 100 100 100
##   8  100 100 100 100 100 100 100 100 100 100
##   9  100 100 100 100 100 100 100 100 100 100
##   10 100 100 100 100 100 100 100 100 100 100
##   11 100 100 100 100 100 100 100 100 100 100
##   12 100 100 100 100 100 100 100 100 100 100
##   13 100 100 100 100 100 100 100 100 100 100
##   14 100 100 100 100 100 100 100 100 100 100
##   15 100 100 100 100 100 100 100 100 100 100
##   16 100 100 100 100 100 100 100 100 100 100
##   17 100 100 100 100 100 100 100 100 100 100
##   18 100 100 100 100 100 100 100 100 100 100
##   19 100 100 100 100 100 100 100 100 100 100
##   20 100 100 100 100 100 100 100 100 100 100
# Kayıp değerlerin yapısına bakalım
md.pattern(fdata)

##       id year sect kg lg mg tc fs re ee lA fe lq lqre lqfe  lk  ll   lm     
## 17229  1    1    1  1  1  1  1  1  1  1  1  1  1    1    1   1   1    1    0
## 865    1    1    1  1  1  1  1  1  1  1  1  1  1    1    1   1   1    0    1
## 882    1    1    1  1  1  1  1  1  1  1  1  1  1    1    1   1   0    1    1
## 69     1    1    1  1  1  1  1  1  1  1  1  1  1    1    1   1   0    0    2
## 842    1    1    1  1  1  1  1  1  1  1  1  1  1    1    1   0   1    1    1
## 76     1    1    1  1  1  1  1  1  1  1  1  1  1    1    1   0   1    0    2
## 29     1    1    1  1  1  1  1  1  1  1  1  1  1    1    1   0   0    1    2
## 8      1    1    1  1  1  1  1  1  1  1  1  1  1    1    1   0   0    0    3
##        0    0    0  0  0  0  0  0  0  0  0  0  0    0    0 955 988 1018 2961
# Kayıp değerlerin olmadığı bir veri seti hazırlayalım
cdata <- na.omit(fdata)
dim(fdata)
## [1] 20000    18
dim(cdata)
## [1] 17229    18
# Çalışma ortamındaki tüm tabloları görelim
tables()
##     NAME   NROW NCOL MB                      COLS KEY
## 1: cdata 17,229   18  2 id,year,sect,kg,lg,mg,...    
## 2: fdata 20,000   18  3 id,year,sect,kg,lg,mg,...    
## Total: 5MB

datatable işlemleri

İşlemlerin yapısı

  • DT[ i, j, by ]
    • DT nesnesini kullam
    • i de tanımlanan satırlar için,
    • j’yi hesapla,
    • bu işlemleri by ile tanımlanan her grup için yap

Değişkenlerin silinmesi

# Bir değişken
fdata[, kg := NULL]
# Birden fazla değişken
fdata[, c("lg", "mg", "tc", "fs", "fe", "ee") := NULL]

Satır işlemleri

  • Satırlar gözlemleri içerir
# 3-5. satırlar
fdata[3:5,]
##    id year sect       re       lk       ll       lm       lA       lq     lqre
## 1:  1    3    1 1.759557 3.942558 3.803200 3.692214 3.741553 7.656327 9.415884
## 2:  1    4    1 1.759557 4.021145       NA 3.711040 3.768173 7.995705 9.755262
## 3:  1    5    1 1.759557 3.984714 3.931298 3.765715 3.813913 7.763024 9.522581
##        lqfe
## 1: 10.79387
## 2: 11.13325
## 3: 10.90057
fdata[3:5]
##    id year sect       re       lk       ll       lm       lA       lq     lqre
## 1:  1    3    1 1.759557 3.942558 3.803200 3.692214 3.741553 7.656327 9.415884
## 2:  1    4    1 1.759557 4.021145       NA 3.711040 3.768173 7.995705 9.755262
## 3:  1    5    1 1.759557 3.984714 3.931298 3.765715 3.813913 7.763024 9.522581
##        lqfe
## 1: 10.79387
## 2: 11.13325
## 3: 10.90057
# id'si 1 olan tüm satırlar
fdata[id == 1]
##     id year sect       re       lk       ll       lm       lA       lq
##  1:  1    1    1 1.759557 3.677162 3.730664 3.720196 3.622019 7.714923
##  2:  1    2    1 1.759557 3.869140 3.798028 3.684258 3.700559 7.557686
##  3:  1    3    1 1.759557 3.942558 3.803200 3.692214 3.741553 7.656327
##  4:  1    4    1 1.759557 4.021145       NA 3.711040 3.768173 7.995705
##  5:  1    5    1 1.759557 3.984714 3.931298 3.765715 3.813913 7.763024
##  6:  1    6    1 1.759557 3.979339 4.000735 3.819774 3.834219 7.866675
##  7:  1    7    1 1.759557 4.101786 4.083978 3.823640 3.853692 7.908060
##  8:  1    8    1 1.759557 4.048504 4.161252 3.822254 3.932113 7.803037
##  9:  1    9    1 1.759557 3.961262 4.166890 3.759391 3.977528 7.972025
## 10:  1   10    1 1.759557 4.068458 4.218723 3.819324 3.993848 7.939336
## 11:  1   11    1 1.759557 4.018583 4.258603 3.810485 4.014686 8.125140
## 12:  1   12    1 1.759557 4.037664 4.284643 3.870614 3.944473 8.009827
## 13:  1   13    1 1.759557 4.181834 4.343615 3.943928 4.006175 8.168516
## 14:  1   14    1 1.759557 4.134559 4.339508 3.939416 4.007519 8.138972
## 15:  1   15    1 1.759557 4.158239 4.370790 3.980651 4.015097 8.286741
## 16:  1   16    1 1.759557 4.192669 4.413958 4.041246 4.001482 8.436208
## 17:  1   17    1 1.759557 4.183384 4.486670 4.127323 4.006634 8.456595
## 18:  1   18    1 1.759557 4.208586 4.538652 4.197928 4.088093 8.684492
## 19:  1   19    1 1.759557 4.286161 4.573148 4.193445 4.125698 8.604524
## 20:  1   20    1 1.759557 4.379317 4.613243 4.160655 4.169695 8.658562
##          lqre     lqfe
##  1:  9.474480 10.85246
##  2:  9.317243 10.69523
##  3:  9.415884 10.79387
##  4:  9.755262 11.13325
##  5:  9.522581 10.90057
##  6:  9.626232 11.00422
##  7:  9.667617 11.04560
##  8:  9.562594 10.94058
##  9:  9.731582 11.10957
## 10:  9.698893 11.07688
## 11:  9.884697 11.26268
## 12:  9.769385 11.14737
## 13:  9.928073 11.30606
## 14:  9.898530 11.27651
## 15: 10.046298 11.42428
## 16: 10.195765 11.57375
## 17: 10.216152 11.59414
## 18: 10.444049 11.82203
## 19: 10.364081 11.74207
## 20: 10.418119 11.79610
setkey(fdata, id, year)
fdata[list(1)]
##     id year sect       re       lk       ll       lm       lA       lq
##  1:  1    1    1 1.759557 3.677162 3.730664 3.720196 3.622019 7.714923
##  2:  1    2    1 1.759557 3.869140 3.798028 3.684258 3.700559 7.557686
##  3:  1    3    1 1.759557 3.942558 3.803200 3.692214 3.741553 7.656327
##  4:  1    4    1 1.759557 4.021145       NA 3.711040 3.768173 7.995705
##  5:  1    5    1 1.759557 3.984714 3.931298 3.765715 3.813913 7.763024
##  6:  1    6    1 1.759557 3.979339 4.000735 3.819774 3.834219 7.866675
##  7:  1    7    1 1.759557 4.101786 4.083978 3.823640 3.853692 7.908060
##  8:  1    8    1 1.759557 4.048504 4.161252 3.822254 3.932113 7.803037
##  9:  1    9    1 1.759557 3.961262 4.166890 3.759391 3.977528 7.972025
## 10:  1   10    1 1.759557 4.068458 4.218723 3.819324 3.993848 7.939336
## 11:  1   11    1 1.759557 4.018583 4.258603 3.810485 4.014686 8.125140
## 12:  1   12    1 1.759557 4.037664 4.284643 3.870614 3.944473 8.009827
## 13:  1   13    1 1.759557 4.181834 4.343615 3.943928 4.006175 8.168516
## 14:  1   14    1 1.759557 4.134559 4.339508 3.939416 4.007519 8.138972
## 15:  1   15    1 1.759557 4.158239 4.370790 3.980651 4.015097 8.286741
## 16:  1   16    1 1.759557 4.192669 4.413958 4.041246 4.001482 8.436208
## 17:  1   17    1 1.759557 4.183384 4.486670 4.127323 4.006634 8.456595
## 18:  1   18    1 1.759557 4.208586 4.538652 4.197928 4.088093 8.684492
## 19:  1   19    1 1.759557 4.286161 4.573148 4.193445 4.125698 8.604524
## 20:  1   20    1 1.759557 4.379317 4.613243 4.160655 4.169695 8.658562
##          lqre     lqfe
##  1:  9.474480 10.85246
##  2:  9.317243 10.69523
##  3:  9.415884 10.79387
##  4:  9.755262 11.13325
##  5:  9.522581 10.90057
##  6:  9.626232 11.00422
##  7:  9.667617 11.04560
##  8:  9.562594 10.94058
##  9:  9.731582 11.10957
## 10:  9.698893 11.07688
## 11:  9.884697 11.26268
## 12:  9.769385 11.14737
## 13:  9.928073 11.30606
## 14:  9.898530 11.27651
## 15: 10.046298 11.42428
## 16: 10.195765 11.57375
## 17: 10.216152 11.59414
## 18: 10.444049 11.82203
## 19: 10.364081 11.74207
## 20: 10.418119 11.79610
fdata[list(c(1:2)), list(id, year, sect, lq)]
##     id year sect       lq
##  1:  1    1    1 7.714923
##  2:  1    2    1 7.557686
##  3:  1    3    1 7.656327
##  4:  1    4    1 7.995705
##  5:  1    5    1 7.763024
##  6:  1    6    1 7.866675
##  7:  1    7    1 7.908060
##  8:  1    8    1 7.803037
##  9:  1    9    1 7.972025
## 10:  1   10    1 7.939336
## 11:  1   11    1 8.125140
## 12:  1   12    1 8.009827
## 13:  1   13    1 8.168516
## 14:  1   14    1 8.138972
## 15:  1   15    1 8.286741
## 16:  1   16    1 8.436208
## 17:  1   17    1 8.456595
## 18:  1   18    1 8.684492
## 19:  1   19    1 8.604524
## 20:  1   20    1 8.658562
## 21:  2    1    1 7.395118
## 22:  2    2    1 7.687345
## 23:  2    3    1 7.602643
## 24:  2    4    1 7.659964
## 25:  2    5    1 7.819811
## 26:  2    6    1 7.808144
## 27:  2    7    1 7.728766
## 28:  2    8    1 7.966463
## 29:  2    9    1 7.829543
## 30:  2   10    1 7.899010
## 31:  2   11    1 7.820649
## 32:  2   12    1 8.018926
## 33:  2   13    1 8.088406
## 34:  2   14    1 8.222450
## 35:  2   15    1 8.045352
## 36:  2   16    1 8.407195
## 37:  2   17    1 8.472305
## 38:  2   18    1 8.552817
## 39:  2   19    1 8.611921
## 40:  2   20    1 8.736459
##     id year sect       lq
fdata[list(c(1:2), c(10,20))]
##    id year sect       re       lk       ll       lm       lA       lq      lqre
## 1:  1   10    1 1.759557 4.068458 4.218723 3.819324 3.993848 7.939336  9.698893
## 2:  2   20    1 1.759557 4.129917 4.378623 4.030449 4.356764 8.736459 10.496016
##        lqfe
## 1: 11.07688
## 2: 12.43439
fdata[(id %in% c(1,2)) & (year %in% c(10, 20))]
##    id year sect       re       lk       ll       lm       lA       lq      lqre
## 1:  1   10    1 1.759557 4.068458 4.218723 3.819324 3.993848 7.939336  9.698893
## 2:  1   20    1 1.759557 4.379317 4.613243 4.160655 4.169695 8.658562 10.418119
## 3:  2   10    1 1.759557 3.859989 4.137080 3.652591 3.976788 7.899010  9.658567
## 4:  2   20    1 1.759557 4.129917 4.378623 4.030449 4.356764 8.736459 10.496016
##        lqfe
## 1: 11.07688
## 2: 11.79610
## 3: 11.59694
## 4: 12.43439
fdata[list(c(1:2))][year %in% c(10, 20)]
##    id year sect       re       lk       ll       lm       lA       lq      lqre
## 1:  1   10    1 1.759557 4.068458 4.218723 3.819324 3.993848 7.939336  9.698893
## 2:  1   20    1 1.759557 4.379317 4.613243 4.160655 4.169695 8.658562 10.418119
## 3:  2   10    1 1.759557 3.859989 4.137080 3.652591 3.976788 7.899010  9.658567
## 4:  2   20    1 1.759557 4.129917 4.378623 4.030449 4.356764 8.736459 10.496016
##        lqfe
## 1: 11.07688
## 2: 11.79610
## 3: 11.59694
## 4: 12.43439
sM <- data.table(id=c(1,1,2,2), year=c(10,20,10,20))
sM
##    id year
## 1:  1   10
## 2:  1   20
## 3:  2   10
## 4:  2   20
fdata[sM]
##    id year sect       re       lk       ll       lm       lA       lq      lqre
## 1:  1   10    1 1.759557 4.068458 4.218723 3.819324 3.993848 7.939336  9.698893
## 2:  1   20    1 1.759557 4.379317 4.613243 4.160655 4.169695 8.658562 10.418119
## 3:  2   10    1 1.759557 3.859989 4.137080 3.652591 3.976788 7.899010  9.658567
## 4:  2   20    1 1.759557 4.129917 4.378623 4.030449 4.356764 8.736459 10.496016
##        lqfe
## 1: 11.07688
## 2: 11.79610
## 3: 11.59694
## 4: 12.43439

Sütun işlemler,

  • Sütunlarda değişkenler yer alır
# id sütununun 3-5. satırları
fdata[3:5, id]
## [1] 1 1 1
# id, year ve lq sütunlarının 3-5. satırları
fdata[3:5, list(id, year, lq)]
##    id year       lq
## 1:  1    3 7.656327
## 2:  1    4 7.995705
## 3:  1    5 7.763024
# NOT: .() list() anlamına gelmektedir

# lk'nin kayıp değerde olduğu gözlemlerin ilk 10 satırı (id, year ve lq değişkenleri)
fdata[is.na(lk), .(id, year, lq, lk)][1:10]
##     id year       lq lk
##  1:  4   11 8.241903 NA
##  2:  5   11 8.291793 NA
##  3:  8    1 7.655521 NA
##  4:  8    2 7.644491 NA
##  5:  8    3 7.686605 NA
##  6:  8    4 7.670166 NA
##  7:  8    5 7.797126 NA
##  8:  8    6 7.693435 NA
##  9:  8    7 7.662222 NA
## 10:  8    8 7.818517 NA
# lq'nin ortalaması
fdata[, mean(lq)]
## [1] 8.198103
# lq'nin ortalaması ve standart hatası
fdata[, .(mean(lq), sd(lq))]
##          V1        V2
## 1: 8.198103 0.4161142
# lk'nin ortalaması ve standart hatası
fdata[, .(mean(lk), sd(lk))]
##    V1 V2
## 1: NA NA
# Yeni değişken yaratmak
fdata[, list(mean.lq = mean(lq), sd.lq = sd(lq))]
##     mean.lq     sd.lq
## 1: 8.198103 0.4161142
# datatable işlemleri olarak fonksiyonlar da kullanılabilir
# do-loop'a gerek yok!
fdata[, {plot(lq)
        plot(ll)}]

## NULL

Grup bazında işlemler

# Grup işlemleri
fdata[, list(mean.lq = mean(lq), sd.lq = sd(lq)), by=sect]
##     sect  mean.lq     sd.lq
##  1:    1 8.180033 0.4082513
##  2:    2 8.200705 0.4193998
##  3:    3 8.194084 0.4190149
##  4:    4 8.214795 0.4258512
##  5:    5 8.185673 0.4028809
##  6:    6 8.190917 0.4099462
##  7:    7 8.189549 0.4106374
##  8:    8 8.193422 0.4184685
##  9:    9 8.240427 0.4299026
## 10:   10 8.191424 0.4136558
# Birden fazla değişken ile gruplama
sectq <- fdata[, list(mean.lq = mean(lq), sd.lq = sd(lq)), by=list(sect, year)]
sectq
##      sect year  mean.lq     sd.lq
##   1:    1    1 7.568505 0.1127734
##   2:    1    2 7.656346 0.1119503
##   3:    1    3 7.724386 0.1114008
##   4:    1    4 7.750268 0.1488633
##   5:    1    5 7.846275 0.1708398
##  ---                             
## 196:   10   16 8.546901 0.2147203
## 197:   10   17 8.620089 0.2005785
## 198:   10   18 8.670703 0.2500211
## 199:   10   19 8.722522 0.2196667
## 200:   10   20 8.781971 0.2416809
tables()
##     NAME   NROW NCOL MB                      COLS     KEY
## 1: cdata 17,229   18  2 id,year,sect,kg,lg,mg,...        
## 2: fdata 20,000   11  1 id,year,sect,re,lk,ll,... id,year
## 3: sectq    200    4  0   sect,year,mean.lq,sd.lq        
## 4:    sM      4    2  0                   id,year        
## Total: 3MB
# by içinde operatör/fınksiyon kullanılması
fdata[, list(mean.lq = mean(lq), sd.lq = sd(lq)), by=list(sect, year>5)]
##     sect  year  mean.lq     sd.lq
##  1:    1 FALSE 7.709156 0.1622812
##  2:    1  TRUE 8.336992 0.3389476
##  3:    2 FALSE 7.707978 0.1620525
##  4:    2  TRUE 8.364947 0.3432946
##  5:    3 FALSE 7.704858 0.1619919
##  6:    3  TRUE 8.357159 0.3449004
##  7:    4 FALSE 7.703277 0.1636964
##  8:    4  TRUE 8.385301 0.3413980
##  9:    5 FALSE 7.715725 0.1554984
## 10:    5  TRUE 8.342322 0.3319376
## 11:    6 FALSE 7.714602 0.1548702
## 12:    6  TRUE 8.349688 0.3394505
## 13:    7 FALSE 7.711210 0.1558822
## 14:    7  TRUE 8.348995 0.3391443
## 15:    8 FALSE 7.707212 0.1547212
## 16:    8  TRUE 8.355492 0.3470176
## 17:    9 FALSE 7.723819 0.1553899
## 18:    9  TRUE 8.412629 0.3460214
## 19:   10 FALSE 7.700174 0.1623720
## 20:   10  TRUE 8.355174 0.3347864
fdata[, list(mean.lq = mean(lq), sd.lq = sd(lq), nobs = length(lq)), by=is.na(ll)]
##    is.na  mean.lq     sd.lq  nobs
## 1: FALSE 8.197396 0.4151473 19012
## 2:  TRUE 8.211708 0.4342968   988
# Seçilen satırlar için gruplama
fdata[1:5, list(mean.lq = mean(lq), sd.lq = sd(lq)), by=year]
##    year  mean.lq sd.lq
## 1:    1 7.714923    NA
## 2:    2 7.557686    NA
## 3:    3 7.656327    NA
## 4:    4 7.995705    NA
## 5:    5 7.763024    NA
# .N ile kayıp gözlem sayısının hesaplanması
fdata[is.na(lk), .N, by = sect]
##     sect   N
##  1:    1 146
##  2:    2  78
##  3:    3  45
##  4:    4  82
##  5:    5  62
##  6:    6 111
##  7:    7  85
##  8:    8 141
##  9:    9  72
## 10:   10 133

Yeni değişken yaratılması

head(fdata)
##    id year sect       re       lk       ll       lm       lA       lq     lqre
## 1:  1    1    1 1.759557 3.677162 3.730664 3.720196 3.622019 7.714923 9.474480
## 2:  1    2    1 1.759557 3.869140 3.798028 3.684258 3.700559 7.557686 9.317243
## 3:  1    3    1 1.759557 3.942558 3.803200 3.692214 3.741553 7.656327 9.415884
## 4:  1    4    1 1.759557 4.021145       NA 3.711040 3.768173 7.995705 9.755262
## 5:  1    5    1 1.759557 3.984714 3.931298 3.765715 3.813913 7.763024 9.522581
## 6:  1    6    1 1.759557 3.979339 4.000735 3.819774 3.834219 7.866675 9.626232
##        lqfe
## 1: 10.85246
## 2: 10.69523
## 3: 10.79387
## 4: 11.13325
## 5: 10.90057
## 6: 11.00422
fdata[, Q1 := exp(lq)]
head(fdata)
##    id year sect       re       lk       ll       lm       lA       lq     lqre
## 1:  1    1    1 1.759557 3.677162 3.730664 3.720196 3.622019 7.714923 9.474480
## 2:  1    2    1 1.759557 3.869140 3.798028 3.684258 3.700559 7.557686 9.317243
## 3:  1    3    1 1.759557 3.942558 3.803200 3.692214 3.741553 7.656327 9.415884
## 4:  1    4    1 1.759557 4.021145       NA 3.711040 3.768173 7.995705 9.755262
## 5:  1    5    1 1.759557 3.984714 3.931298 3.765715 3.813913 7.763024 9.522581
## 6:  1    6    1 1.759557 3.979339 4.000735 3.819774 3.834219 7.866675 9.626232
##        lqfe       Q1
## 1: 10.85246 2241.550
## 2: 10.69523 1915.408
## 3: 10.79387 2113.979
## 4: 11.13325 2968.181
## 5: 10.90057 2352.006
## 6: 11.00422 2608.877
# Yeni değişken yaratıldığında DT <- DT[...] işlemi gerekli değil!
# := yeni değişkenleri DT içinde oluşturur
fdata[, c("K", "L", "M") := list(exp(lk), exp(ll), exp(lm))]
head(fdata)
##    id year sect       re       lk       ll       lm       lA       lq     lqre
## 1:  1    1    1 1.759557 3.677162 3.730664 3.720196 3.622019 7.714923 9.474480
## 2:  1    2    1 1.759557 3.869140 3.798028 3.684258 3.700559 7.557686 9.317243
## 3:  1    3    1 1.759557 3.942558 3.803200 3.692214 3.741553 7.656327 9.415884
## 4:  1    4    1 1.759557 4.021145       NA 3.711040 3.768173 7.995705 9.755262
## 5:  1    5    1 1.759557 3.984714 3.931298 3.765715 3.813913 7.763024 9.522581
## 6:  1    6    1 1.759557 3.979339 4.000735 3.819774 3.834219 7.866675 9.626232
##        lqfe       Q1        K        L        M
## 1: 10.85246 2241.550 39.53405 41.70678 41.27247
## 2: 10.69523 1915.408 47.90119 44.61312 39.81558
## 3: 10.79387 2113.979 51.55027 44.84447 40.13360
## 4: 11.13325 2968.181 55.76489       NA 40.89631
## 5: 10.90057 2352.006 53.76991 50.97312 43.19459
## 6: 11.00422 2608.877 53.48165 54.63832 45.59392

Endeksleme ve anahtarlar

setkey(fdata, id, year)
fdata[.(1), .(id, year, Q1)]
##     id year       Q1
##  1:  1    1 2241.550
##  2:  1    2 1915.408
##  3:  1    3 2113.979
##  4:  1    4 2968.181
##  5:  1    5 2352.006
##  6:  1    6 2608.877
##  7:  1    7 2719.109
##  8:  1    8 2448.024
##  9:  1    9 2898.721
## 10:  1   10 2805.497
## 11:  1   11 3378.340
## 12:  1   12 3010.398
## 13:  1   13 3528.104
## 14:  1   14 3425.396
## 15:  1   15 3970.871
## 16:  1   16 4611.036
## 17:  1   17 4706.006
## 18:  1   18 5910.536
## 19:  1   19 5456.289
## 20:  1   20 5759.246
fdata[.(c(1:10)), mult = "first", .(id, year, Q1)]
##     id year       Q1
##  1:  1    1 2241.550
##  2:  2    1 1628.017
##  3:  3    1 2165.505
##  4:  4    1 1715.678
##  5:  5    1 1691.209
##  6:  6    1 1707.853
##  7:  7    1 1840.785
##  8:  8    1 2112.276
##  9:  9    1 2536.401
## 10: 10    1 2100.196
fdata[.(c(1:10)), mult = "last", .(id, year, Q1)]
##     id year        Q1
##  1:  1   20  5759.246
##  2:  2   20  6225.813
##  3:  3   20  7385.818
##  4:  4   20  6731.757
##  5:  5   20  8693.780
##  6:  6   20  6411.944
##  7:  7   20  7293.666
##  8:  8   20  8070.181
##  9:  9   20 11502.584
## 10: 10   20  6146.992
fdata[.(1,20)]
##    id year sect       re       lk       ll       lm       lA       lq     lqre
## 1:  1   20    1 1.759557 4.379317 4.613243 4.160655 4.169695 8.658562 10.41812
##       lqfe       Q1       K        L        M
## 1: 11.7961 5759.246 79.7835 100.8106 64.11348
fdata[.(c(1:10)), sum(Q1), by = .EACHI]
##     id       V1
##  1:  1 68827.58
##  2:  2 65040.03
##  3:  3 83227.48
##  4:  4 75885.58
##  5:  5 92356.68
##  6:  6 75939.10
##  7:  7 84751.83
##  8:  8 73023.61
##  9:  9 96708.35
## 10: 10 73910.11

Değişken ismi değiştirilmesi

setnames(fdata, "Q1", "Q")
# Değişken ismini değiştir
fdata[, list(sum(Q), round(sum(L, na.rm = TRUE), 0)), by = year]
##     year      V1    V2
##  1:    1 1962948 38522
##  2:    2 2105230 40088
##  3:    3 2246275 41785
##  4:    4 2400973 43720
##  5:    5 2578635 45726
##  6:    6 2740260 46761
##  7:    7 2946779 49223
##  8:    8 3136444 51582
##  9:    9 3374734 53495
## 10:   10 3592687 55608
## 11:   11 3852486 58316
## 12:   12 4105420 61087
## 13:   13 4345016 62950
## 14:   14 4626340 66019
## 15:   15 4971910 68807
## 16:   16 5284673 71770
## 17:   17 5685954 74776
## 18:   18 6039388 77633
## 19:   19 6465543 81118
## 20:   20 6906750 83643
# Ve sırala
fdata[order(-year)]
##          id year sect       re       lk       ll       lm       lA       lq
##     1:    1   20    1 1.759557 4.379317 4.613243 4.160655 4.169695 8.658562
##     2:    2   20    1 1.759557 4.129917 4.378623 4.030449 4.356764 8.736459
##     3:    3   20    1 1.759557 4.043933 4.242055 4.285151 4.344415 8.907317
##     4:    4   20    1 1.759557 4.724215 4.473943 4.052891 4.437002 8.814591
##     5:    5   20    1 1.759557 3.960350 4.607179 4.028273 4.647148 9.070363
##    ---                                                                     
## 19996:  996    1   10 1.759557 3.606828 3.690967 3.725613 3.732710 7.684911
## 19997:  997    1   10 1.759557 3.787980 3.643523 3.727933 3.614846 7.635785
## 19998:  998    1   10 1.759557 3.700249 3.624219 3.659327 3.642017 7.488636
## 19999:  999    1   10 1.759557 3.705054 3.747131 3.680591 3.679624 7.525502
## 20000: 1000    1   10 1.759557 3.762371 3.680319       NA 3.710717 7.470778
##             lqre     lqfe        Q         K         L        M
##     1: 10.418119 11.79610 5759.246  79.78350 100.81059 64.11348
##     2: 10.496016 12.43439 6225.813  62.17276  79.72818 56.28619
##     3: 10.666874 12.09744 7385.818  57.05029  69.55063 72.61354
##     4: 10.574149 12.18435 6731.757 112.64198  87.70183 57.56362
##     5: 10.829920 12.22065 8693.780  52.47571 100.20108 56.16382
##    ---                                                         
## 19996:  9.444468 11.07915 2175.275  36.84900  40.08358 41.49665
## 19997:  9.395342 10.83997 2070.995  44.16711  38.22628 41.59306
## 19998:  9.248194 10.67783 1787.613  40.45739  37.49543 38.83520
## 19999:  9.285059 10.76246 1854.744  40.65225  42.39926 39.66984
## 20000:  9.230335 10.77457 1755.972  43.05040  39.65905       NA

Zincirleme işlemler

# İki işlemi aynı anda yap
fdata[, .(sum(Q), round(sum(L, na.rm = TRUE), 0)), by = year][order(-year)]
##     year      V1    V2
##  1:   20 6906750 83643
##  2:   19 6465543 81118
##  3:   18 6039388 77633
##  4:   17 5685954 74776
##  5:   16 5284673 71770
##  6:   15 4971910 68807
##  7:   14 4626340 66019
##  8:   13 4345016 62950
##  9:   12 4105420 61087
## 10:   11 3852486 58316
## 11:   10 3592687 55608
## 12:    9 3374734 53495
## 13:    8 3136444 51582
## 14:    7 2946779 49223
## 15:    6 2740260 46761
## 16:    5 2578635 45726
## 17:    4 2400973 43720
## 18:    3 2246275 41785
## 19:    2 2105230 40088
## 20:    1 1962948 38522

Verilerin şekillendirilmesi

  • Verilerin eritilmesi (melting): Uzun format
  • Verilerin kalıba dökülmesi (casting): Geniş format
# Sektörel fiyatları oluşturalım
sPrice <- data.table(
    year = c(1:nt),
    p1 = 1 + cumsum(rnorm(nt, 0.02, 0.04)),
    p2 = 1 + cumsum(rnorm(nt, 0.03, 0.05)),
    p3 = 1 + cumsum(rnorm(nt, 0.04, 0.02)),
    p4 = 1 + cumsum(rnorm(nt, 0.05, 0.03)),
    p5 = 1 + cumsum(rnorm(nt, 0.02, 0.04)),
    p6 = 1 + cumsum(rnorm(nt, 0.03, 0.06)),
    p7 = 1 + cumsum(rnorm(nt, 0.04, 0.02)),
    p8 = 1 + cumsum(rnorm(nt, 0.05, 0.07)),
    p9 = 1 + cumsum(rnorm(nt, 0.05, 0.04)),
    p10 = 1 + cumsum(rnorm(nt, 0.05, 0.06)))

head(sPrice)
##    year       p1       p2       p3       p4       p5       p6       p7       p8
## 1:    1 1.084834 1.086403 1.044239 1.039482 1.120569 1.083191 1.036712 1.093676
## 2:    2 1.101266 1.166263 1.087915 1.066618 1.242368 1.160425 1.069804 1.201331
## 3:    3 1.164072 1.226919 1.125465 1.120647 1.222590 1.223205 1.112062 1.288835
## 4:    4 1.164269 1.318204 1.136977 1.167959 1.200625 1.335707 1.147675 1.337262
## 5:    5 1.200572 1.355829 1.178433 1.204771 1.217625 1.290941 1.206488 1.412035
## 6:    6 1.244234 1.358888 1.192575 1.299423 1.309964 1.434764 1.258097 1.402196
##          p9       p10
## 1: 1.093882 1.0195968
## 2: 1.161360 1.0470296
## 3: 1.185696 0.9921116
## 4: 1.220048 1.1548797
## 5: 1.296979 1.2125423
## 6: 1.350105 1.2534466
sPriceM <- melt(sPrice, id.vars=1, variable.name = "sectName", value.name = "price")
head(sPriceM)
##    year sectName    price
## 1:    1       p1 1.084834
## 2:    2       p1 1.101266
## 3:    3       p1 1.164072
## 4:    4       p1 1.164269
## 5:    5       p1 1.200572
## 6:    6       p1 1.244234
sPriceM$sect <- as.numeric(unlist(gsub("[^0-9]", "", unlist(sPriceM$sectName)), ""))
sPriceM[, sectName := NULL]
head(sPriceM)
##    year    price sect
## 1:    1 1.084834    1
## 2:    2 1.101266    1
## 3:    3 1.164072    1
## 4:    4 1.164269    1
## 5:    5 1.200572    1
## 6:    6 1.244234    1
ggplot(sPriceM, aes(x=year, y=price, group=sect, color=as.factor(sect))) + geom_line() +
    ggtitle("Price indices")

setkey(sPriceM, sect, year)
setkey(fdata, sect, year)
fdataM <- fdata[sPriceM]
head(fdataM)
##    id year sect       re       lk       ll       lm       lA       lq     lqre
## 1:  1    1    1 1.759557 3.677162 3.730664 3.720196 3.622019 7.714923 9.474480
## 2:  2    1    1 1.759557 3.771031 3.719085 3.693771 3.704430 7.395118 9.154675
## 3:  3    1    1 1.759557 3.779838 3.614786 3.706404 3.752991 7.680409 9.439966
## 4:  4    1    1 1.759557 3.704125 3.617248 3.570059 3.709631 7.447564 9.207121
## 5:  5    1    1 1.759557 3.619427 3.663226 3.716082 3.730261 7.433199 9.192756
## 6:  6    1    1 1.759557 3.861231       NA 3.612980 3.718183 7.442992 9.202550
##        lqfe        Q        K        L        M    price
## 1: 10.85246 2241.550 39.53405 41.70678 41.27247 1.084834
## 2: 11.09305 1628.017 43.42479 41.22665 40.19616 1.084834
## 3: 10.87053 2165.505 43.80896 37.14340 40.70714 1.084834
## 4: 10.81732 1715.678 40.61450 37.23495 35.51868 1.084834
## 5: 10.58349 1691.209 37.31618 38.98692 41.10305 1.084834
## 6: 10.63054 1707.853 47.52379       NA 37.07637 1.084834

Veri setlerinib birleştirilmesi

  • Sektör düzeyinde fiyatları oluşturalım
sdata <- fdata[, .(sQ = sum(Q, na.rm=T), sL = sum(L, na.rm=T), sM = sum(M, na.rm=T)),
    by = .(sect, year)]
summary(sdata)
##       sect           year             sQ               sL             sM      
##  Min.   : 1.0   Min.   : 1.00   Min.   :194352   Min.   :3730   Min.   :3616  
##  1st Qu.: 3.0   1st Qu.: 5.75   1st Qu.:267073   1st Qu.:4620   1st Qu.:4216  
##  Median : 5.5   Median :10.50   Median :370984   Median :5692   Median :4742  
##  Mean   : 5.5   Mean   :10.50   Mean   :396842   Mean   :5863   Mean   :4756  
##  3rd Qu.: 8.0   3rd Qu.:15.25   3rd Qu.:515701   3rd Qu.:7037   3rd Qu.:5272  
##  Max.   :10.0   Max.   :20.00   Max.   :748102   Max.   :8758   Max.   :6069
dim(sdata)
## [1] 200   5
# Sektörel fiyatları firma verileri ile birleştir
setkey(fdata, sect, year)
setkey(sdata, sect, year)
hdata <- fdata[sdata]
dim(hdata)
## [1] 20000    18
dim(fdata)
## [1] 20000    15
identical(hdata$ll, fdata$ll)
## [1] TRUE
# Daha kısa yol
fdata[, c("sQ", "sL", "sM") := list(sum(Q, na.rm=T), sum(L, na.rm=T), sum(M, na.rm=T)),
    by = .(sect, year)]
identical(hdata, fdata)
## [1] FALSE
# Sektörel geometrik ortalamalardan farkı bul
fdata[, lqdev := lq - mean(lq, na.rm=T), by = .(sect)]
fdata[, lkdev := lk - mean(lk, na.rm=T), by = .(sect)]
fdata[, lldev := ll - mean(ll, na.rm=T), by = .(sect)]
fdata[, lmdev := lm - mean(lm, na.rm=T), by = .(sect)]

Regresyon analizi

Model ve tahmin

model1 <- lm(lq ~ lk + ll + lm + year, data = fdata)
model2 <- lm(lqdev ~ lkdev + lldev + lmdev + year, data = fdata)
model3 <- lm(lqre ~ lk + ll + lm + year, data = fdata)
model4 <- lm(lqfe ~ lk + ll + lm + year, data = fdata)
model5 <- plm(lq ~ lk + ll + lm + year, data = fdata, model="random", effect="individual")
model6 <- plm(lq ~ lk + ll + lm + year, data = fdata, model="within", effect="individual")
stargazer(model1, model2, model3, model4, model5, model6, type="html",
    model.numbers=FALSE, 
    dep.var.labels.include = FALSE, dep.var.caption  = "Production function estimates", 
    column.labels = c("OLS", "OLS+dev", "OLS RE", "OLS FE", "Random effects", "Fixed effects"))
Production function estimates
OLS OLS OLS OLS panel
linear
OLS OLS+dev OLS RE OLS FE Random effects Fixed effects
lk 0.112*** 0.112*** 0.246*** 0.110*** 0.110***
(0.005) (0.005) (0.008) (0.007) (0.007)
ll 0.198*** 0.198*** 0.366*** 0.214*** 0.218***
(0.010) (0.010) (0.014) (0.013) (0.014)
lm 0.745*** 0.745*** 0.684*** 0.762*** 0.766***
(0.011) (0.011) (0.015) (0.013) (0.014)
lkdev 0.112***
(0.005)
lldev 0.196***
(0.010)
lmdev 0.744***
(0.011)
year 0.036*** 0.036*** 0.036*** 0.026***
(0.0005) (0.0005) (0.0005) (0.001)
year2 0.037*** 0.037***
(0.006) (0.006)
year3 0.070*** 0.069***
(0.006) (0.006)
year4 0.105*** 0.104***
(0.006) (0.006)
year5 0.149*** 0.148***
(0.006) (0.006)
year6 0.175*** 0.174***
(0.007) (0.007)
year7 0.220*** 0.219***
(0.007) (0.007)
year8 0.251*** 0.249***
(0.007) (0.007)
year9 0.289*** 0.288***
(0.008) (0.008)
year10 0.320*** 0.318***
(0.008) (0.008)
year11 0.359*** 0.357***
(0.008) (0.009)
year12 0.394*** 0.391***
(0.009) (0.009)
year13 0.419*** 0.416***
(0.009) (0.010)
year14 0.454*** 0.451***
(0.010) (0.010)
year15 0.499*** 0.495***
(0.010) (0.011)
year16 0.526*** 0.523***
(0.010) (0.011)
year17 0.567*** 0.563***
(0.011) (0.012)
year18 0.596*** 0.592***
(0.011) (0.012)
year19 0.634*** 0.630***
(0.012) (0.013)
year20 0.668*** 0.663***
(0.012) (0.013)
Constant 3.651*** -0.380*** 5.410*** 6.008*** 3.568***
(0.051) (0.005) (0.051) (0.075) (0.066)
Observations 17,229 17,229 17,229 17,229 17,229 17,229
R2 0.859 0.860 0.859 0.746 0.908 0.910
Adjusted R2 0.859 0.860 0.859 0.746 0.908 0.905
Residual Std. Error (df = 17224) 0.156 0.156 0.156 0.228
F Statistic 26,201.980*** (df = 4; 17224) 26,367.730*** (df = 4; 17224) 26,201.980*** (df = 4; 17224) 12,634.110*** (df = 4; 17224) 164,933.400*** 7,470.656*** (df = 22; 16278)
Note: p<0.1; p<0.05; p<0.01

Regresyon sonuçları

attributes(model1)
## $names
##  [1] "coefficients"  "residuals"     "effects"       "rank"         
##  [5] "fitted.values" "assign"        "qr"            "df.residual"  
##  [9] "na.action"     "xlevels"       "call"          "terms"        
## [13] "model"        
## 
## $class
## [1] "lm"
attributes(model5)
## $names
##  [1] "coefficients" "vcov"         "residuals"    "df.residual"  "formula"     
##  [6] "model"        "ercomp"       "assign"       "contrasts"    "args"        
## [11] "aliased"      "call"        
## 
## $class
## [1] "plm"        "panelmodel"
class(model1$residuals)
## [1] "numeric"
hist(model1$residuals, plot = TRUE)

Monte Carlo analizi

  • Örneklem büyüklüğü ve tahmin hatası arasındaki ilişki
ntest <- 1000
coeffA <- matrix(NA, nrow=ntest, ncol=5)
coeffB <- matrix(NA, nrow=ntest, ncol=5)
for (j in c(1:5)) {
    for (i in c(1:ntest)) {
        ns <- floor(10*exp(j))
        x1 <- rnorm(ns, 0, 1)
        x2 <- rnorm(ns, 0, 2)
        y <- 1 + 0.5*x1 + 2*x2 + rnorm(ns, 0, 1)
        mod <- lm(y ~ x1 + x2)
        coeffA[i,j] <- mod$coefficients[2]
        coeffB[i,j] <- mod$coefficients[3]
    }
}
coeffA <- as.data.frame(coeffA)
coeffB <- as.data.frame(coeffB)
names(coeffB) <- names(coeffA) <- as.character(floor(10*exp(c(1:5))))
head(coeffA)
##          27        73       200       545      1484
## 1 0.2922516 0.7159659 0.3938043 0.5375261 0.4639097
## 2 0.4407526 0.4284639 0.4740828 0.5068020 0.4948005
## 3 0.6378205 0.6418433 0.5092009 0.5826468 0.4872815
## 4 0.3617704 0.3067727 0.4320805 0.4950480 0.4766151
## 5 0.3642107 0.5168827 0.4406001 0.5524684 0.4907939
## 6 0.1774476 0.4594442 0.5708598 0.5133530 0.4686676
coeffAA <- melt(coeffA, id.vars=0, variable.name = "N_obs", value.name = "Estimate")
## Warning in melt(coeffA, id.vars = 0, variable.name = "N_obs", value.name =
## "Estimate"): The melt generic in data.table has been passed a data.frame and
## will attempt to redirect to the relevant reshape2 method; please note that
## reshape2 is deprecated, and this redirection is now deprecated as well. To
## continue using melt methods from reshape2 while both libraries are attached,
## e.g. melt.list, you can prepend the namespace like reshape2::melt(coeffA). In
## the next version, this warning will become an error.
head(coeffAA)
##   N_obs  Estimate
## 1    27 0.2922516
## 2    27 0.4407526
## 3    27 0.6378205
## 4    27 0.3617704
## 5    27 0.3642107
## 6    27 0.1774476
coeffBB <- melt(coeffB, id.vars=0, variable.name = "N_obs", value.name = "Estimate")
## Warning in melt(coeffB, id.vars = 0, variable.name = "N_obs", value.name =
## "Estimate"): The melt generic in data.table has been passed a data.frame and
## will attempt to redirect to the relevant reshape2 method; please note that
## reshape2 is deprecated, and this redirection is now deprecated as well. To
## continue using melt methods from reshape2 while both libraries are attached,
## e.g. melt.list, you can prepend the namespace like reshape2::melt(coeffB). In
## the next version, this warning will become an error.
ggplot(coeffAA, aes(x=Estimate, group=N_obs, color=N_obs)) + geom_density() + 
    ggtitle("Coefficient A")

ggplot(coeffBB, aes(x=Estimate, group=N_obs, color=N_obs)) + geom_density() + 
    ggtitle("Coefficient B")