R Markdown

Dogrusal_reg_se <- function(X, Y)
  sqrt(as.numeric(var(Y - X%*%solve(t(X) %*% X)%*%t(X) %*% Y))*solve(t(X) %*% X))
dr <- function(X, Y) {
  A <- solve(t(X) %*% X)%*%t(X) %*% Y
  B <- diag(sqrt(as.numeric(t(Y-as.numeric(t(Y)%*%rep(1, nrow(Y))/nrow(Y)))%*%(Y-as.numeric(t(Y)%*%rep(1, nrow(Y))/nrow(Y)))/(nrow(Y)-1))*solve(t(X) %*% X)))
  result <- list(t(A), B)
  names(result) <- c("katsayılar", "Standart Hatalar")
  suppressMessages(return(result))
}
library(wooldridge) # data komutuyla fertil1 verisetini indirmek içim
library(rmarkdown) # page_table komutuyla veri setini rmarkdown üzerinde gösterebilmek için
data("fertil1")
paged_table(fertil1)
require(dplyr)
## Zorunlu paket yükleniyor: dplyr
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
fertil1 %>% 
  group_by(year) %>% 
  summarise("ortalama çocuk sayısı (kids)" = mean(kids))
## # A tibble: 7 × 2
##    year `ortalama çocuk sayısı (kids)`
##   <int>                          <dbl>
## 1    72                           3.03
## 2    74                           3.21
## 3    76                           2.80
## 4    78                           2.80
## 5    80                           2.82
## 6    82                           2.40
## 7    84                           2.24
require(dplyr)
fertil1 %>% 
  group_by(year) %>% 
  summarise(across(everything(), mean))
## # A tibble: 7 × 27
##    year  educ meduc feduc   age  kids  black  east northcen   west  farm othru…¹
##   <int> <dbl> <dbl> <dbl> <dbl> <dbl>  <dbl> <dbl>    <dbl>  <dbl> <dbl>   <dbl>
## 1    72  12.2  8.33  8.90  44.9  3.03 0.0833 0.333    0.231 0.128  0.173  0.0833
## 2    74  12.3  8.94  9.29  44.1  3.21 0.0578 0.237    0.353 0.110  0.208  0.110 
## 3    76  12.2  8.25  8.99  43.5  2.80 0.0461 0.263    0.316 0.0855 0.237  0.112 
## 4    78  12.6  9.07  9.80  43.4  2.80 0.0490 0.273    0.329 0.105  0.203  0.112 
## 5    80  12.9  9.40  9.95  43.7  2.82 0.0704 0.141    0.394 0.155  0.218  0.106 
## 6    82  13.2  9.56 10.2   43.2  2.40 0.199  0.231    0.290 0.0806 0.167  0.118 
## 7    84  13.3 10.2  10.7   41.8  2.24 0.0678 0.260    0.333 0.102  0.192  0.0734
## # … with 15 more variables: town <dbl>, smcity <dbl>, y74 <dbl>, y76 <dbl>,
## #   y78 <dbl>, y80 <dbl>, y82 <dbl>, y84 <dbl>, agesq <dbl>, y74educ <dbl>,
## #   y76educ <dbl>, y78educ <dbl>, y80educ <dbl>, y82educ <dbl>, y84educ <dbl>,
## #   and abbreviated variable name ¹​othrural
require(dplyr)
fertil1 %>% 
  group_by(year) %>% 
  summarise("ortalama çocuk sayısı (kids)" = mean(kids), "stadandart sapma çocuk sayısı (kids)" = sd(kids) )
## # A tibble: 7 × 3
##    year `ortalama çocuk sayısı (kids)` `stadandart sapma çocuk sayısı (kids)`
##   <int>                          <dbl>                                  <dbl>
## 1    72                           3.03                                   1.83
## 2    74                           3.21                                   1.50
## 3    76                           2.80                                   1.66
## 4    78                           2.80                                   1.58
## 5    80                           2.82                                   1.58
## 6    82                           2.40                                   1.70
## 7    84                           2.24                                   1.51
summary(lm(kids ~ educ + age + I(age^2) + black + east + northcen + west + farm + othrural + town + smcity + y74 + y76 + y78 + y80 + y82 + y84, data = fertil1))
## 
## Call:
## lm(formula = kids ~ educ + age + I(age^2) + black + east + northcen + 
##     west + farm + othrural + town + smcity + y74 + y76 + y78 + 
##     y80 + y82 + y84, data = fertil1)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.9878 -1.0086 -0.0767  0.9331  4.6548 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -7.742457   3.051767  -2.537 0.011315 *  
## educ        -0.128427   0.018349  -6.999 4.44e-12 ***
## age          0.532135   0.138386   3.845 0.000127 ***
## I(age^2)    -0.005804   0.001564  -3.710 0.000217 ***
## black        1.075658   0.173536   6.198 8.02e-10 ***
## east         0.217324   0.132788   1.637 0.101992    
## northcen     0.363114   0.120897   3.004 0.002729 ** 
## west         0.197603   0.166913   1.184 0.236719    
## farm        -0.052557   0.147190  -0.357 0.721105    
## othrural    -0.162854   0.175442  -0.928 0.353481    
## town         0.084353   0.124531   0.677 0.498314    
## smcity       0.211879   0.160296   1.322 0.186507    
## y74          0.268183   0.172716   1.553 0.120771    
## y76         -0.097379   0.179046  -0.544 0.586633    
## y78         -0.068666   0.181684  -0.378 0.705544    
## y80         -0.071305   0.182771  -0.390 0.696511    
## y82         -0.522484   0.172436  -3.030 0.002502 ** 
## y84         -0.545166   0.174516  -3.124 0.001831 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.555 on 1111 degrees of freedom
## Multiple R-squared:  0.1295, Adjusted R-squared:  0.1162 
## F-statistic: 9.723 on 17 and 1111 DF,  p-value: < 2.2e-16
data("cps78_85")
paged_table(cps78_85)
cps78_85 %>% 
  group_by(year) %>% 
  summarise(n = n())
## # A tibble: 2 × 2
##    year     n
##   <int> <int>
## 1    78   550
## 2    85   534
summary(lm(lwage ~ y85*(educ + female) + exper + I(exper^2) + union, data = cps78_85))
## 
## Call:
## lm(formula = lwage ~ y85 * (educ + female) + exper + I(exper^2) + 
##     union, data = cps78_85)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.56098 -0.25828  0.00864  0.26571  2.11669 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  4.589e-01  9.345e-02   4.911 1.05e-06 ***
## y85          1.178e-01  1.238e-01   0.952   0.3415    
## educ         7.472e-02  6.676e-03  11.192  < 2e-16 ***
## female      -3.167e-01  3.662e-02  -8.648  < 2e-16 ***
## exper        2.958e-02  3.567e-03   8.293 3.27e-16 ***
## I(exper^2)  -3.994e-04  7.754e-05  -5.151 3.08e-07 ***
## union        2.021e-01  3.029e-02   6.672 4.03e-11 ***
## y85:educ     1.846e-02  9.354e-03   1.974   0.0487 *  
## y85:female   8.505e-02  5.131e-02   1.658   0.0977 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.4127 on 1075 degrees of freedom
## Multiple R-squared:  0.4262, Adjusted R-squared:  0.4219 
## F-statistic:  99.8 on 8 and 1075 DF,  p-value: < 2.2e-16
data("kielmc")
paged_table(kielmc)
kielmc81 <- kielmc %>% filter(year==1981)
model_81 <- lm(kielmc81$rprice ~ kielmc81$nearinc)
summary(model_81)
## 
## Call:
## lm(formula = kielmc81$rprice ~ kielmc81$nearinc)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -60678 -19832  -2997  21139 136754 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        101308       3093  32.754  < 2e-16 ***
## kielmc81$nearinc   -30688       5828  -5.266 5.14e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 31240 on 140 degrees of freedom
## Multiple R-squared:  0.1653, Adjusted R-squared:  0.1594 
## F-statistic: 27.73 on 1 and 140 DF,  p-value: 5.139e-07
kielmc78 <- kielmc %>% filter(year==1978)
model_78 <- lm(kielmc78$rprice ~ kielmc78$nearinc)
summary(model_78)
## 
## Call:
## lm(formula = kielmc78$rprice ~ kielmc78$nearinc)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -56517 -16605  -3193   8683 236307 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)         82517       2654  31.094  < 2e-16 ***
## kielmc78$nearinc   -18824       4745  -3.968 0.000105 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 29430 on 177 degrees of freedom
## Multiple R-squared:  0.08167,    Adjusted R-squared:  0.07648 
## F-statistic: 15.74 on 1 and 177 DF,  p-value: 0.0001054
model_78_81 <- lm(kielmc$rprice ~ kielmc$nearinc*kielmc$y81)
summary(model_78_81)
## 
## Call:
## lm(formula = kielmc$rprice ~ kielmc$nearinc * kielmc$y81)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -60678 -17693  -3031  12483 236307 
## 
## Coefficients:
##                           Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                  82517       2727  30.260  < 2e-16 ***
## kielmc$nearinc              -18824       4875  -3.861 0.000137 ***
## kielmc$y81                   18790       4050   4.640 5.12e-06 ***
## kielmc$nearinc:kielmc$y81   -11864       7457  -1.591 0.112595    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 30240 on 317 degrees of freedom
## Multiple R-squared:  0.1739, Adjusted R-squared:  0.1661 
## F-statistic: 22.25 on 3 and 317 DF,  p-value: 4.224e-13
library(stargazer)
## 
## Please cite as:
##  Hlavac, Marek (2022). stargazer: Well-Formatted Regression and Summary Statistics Tables.
##  R package version 5.2.3. https://CRAN.R-project.org/package=stargazer
stargazer(list(model_78,model_81,model_78_81), type = "text")
## 
## ===========================================================================================
##                                               Dependent variable:                          
##                     -----------------------------------------------------------------------
##                             rprice                  rprice                  rprice         
##                               (1)                     (2)                     (3)          
## -------------------------------------------------------------------------------------------
## nearinc                 -18,824.370***                                                     
##                           (4,744.594)                                                      
##                                                                                            
## nearinc                                         -30,688.270***                             
##                                                   (5,827.709)                              
##                                                                                            
## nearinc                                                                 -18,824.370***     
##                                                                           (4,875.322)      
##                                                                                            
## y81                                                                      18,790.290***     
##                                                                           (4,050.065)      
##                                                                                            
## y81                                                                       -11,863.900      
##                                                                           (7,456.646)      
##                                                                                            
## Constant                 82,517.230***          101,307.500***           82,517.230***     
##                           (2,653.790)             (3,093.027)             (2,726.910)      
##                                                                                            
## -------------------------------------------------------------------------------------------
## Observations                  179                     142                     321          
## R2                           0.082                   0.165                   0.174         
## Adjusted R2                  0.076                   0.159                   0.166         
## Residual Std. Error  29,431.960 (df = 177)   31,238.040 (df = 140)   30,242.900 (df = 317) 
## F Statistic         15.741*** (df = 1; 177) 27.730*** (df = 1; 140) 22.251*** (df = 3; 317)
## ===========================================================================================
## Note:                                                           *p<0.1; **p<0.05; ***p<0.01
model_78_81_sutun_2 <- lm(rprice ~ nearinc*y81 + age + I(age^2), data = kielmc )
summary(model_78_81_sutun_2)
## 
## Call:
## lm(formula = rprice ~ nearinc * y81 + age + I(age^2), data = kielmc)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -79349 -14431  -1711  10069 201486 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  8.912e+04  2.406e+03  37.039  < 2e-16 ***
## nearinc      9.398e+03  4.812e+03   1.953 0.051713 .  
## y81          2.132e+04  3.444e+03   6.191 1.86e-09 ***
## age         -1.494e+03  1.319e+02 -11.333  < 2e-16 ***
## I(age^2)     8.691e+00  8.481e-01  10.248  < 2e-16 ***
## nearinc:y81 -2.192e+04  6.360e+03  -3.447 0.000644 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 25540 on 315 degrees of freedom
## Multiple R-squared:  0.4144, Adjusted R-squared:  0.4052 
## F-statistic: 44.59 on 5 and 315 DF,  p-value: < 2.2e-16