Last active
December 27, 2016 14:58
-
-
Save jonnyrobbie/844591e75f037dd2f5224ce6914cc4f1 to your computer and use it in GitHub Desktop.
R script for simple OLS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Year | M_1_length_of_routes | M_2_vehicles | M_3_passengers | M_4_avg_speed | M_5_place_km | S_6_length_of_routes | S_7_vehicles | S_8_passengers | S_9_avg_speed | S_10_place_km | T_11_passengers | T_12_Prague_population | T_13_avg_income_Prague | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 2001 | 50.1 | 490 | 442448000 | 35.36 | 7132399000 | 2703.8 | 2266 | 661891000 | 22.54 | 10359943000 | 1104339000 | 1160118 | 18442 | |
| 2002 | 50.1 | 491 | 416516000 | 35.36 | 6443208000 | 2725.7 | 2308 | 699093000 | 22.71 | 11075738000 | 1115609000 | 1161938 | 19926 | |
| 2003 | 50.1 | 514 | 458642000 | 35.7 | 7290246000 | 2731.1 | 2290 | 672146000 | 22.95 | 10720017000 | 1130788000 | 1165581 | 21114 | |
| 2004 | 54 | 490 | 496013000 | 34.6 | 8215418000 | 2677.3 | 2221 | 664519000 | 22.7 | 10661613000 | 1160532000 | 1170571 | 22204 | |
| 2005 | 54 | 420 | 515098000 | 34.6 | 8632642000 | 2643.8 | 2149 | 632302000 | 22.9 | 10661296000 | 1147400000 | 1181610 | 23367 | |
| 2006 | 54.9 | 555 | 531239000 | 34.6 | 8703277000 | 2682.7 | 2161 | 650166000 | 22.35 | 10702364000 | 1181405000 | 1188126 | 25001 | |
| 2007 | 54.9 | 567 | 537266000 | 35.8 | 8721653000 | 2655.9 | 2138 | 655660000 | 22.25 | 10690456000 | 1192926000 | 1212097 | 26566 | |
| 2008 | 59.4 | 576 | 596893000 | 35.51 | 9801991000 | 2452.6 | 2094 | 664946000 | 22.05 | 10894615000 | 1261839000 | 1233211 | 29031 | |
| 2009 | 59.4 | 610 | 584880000 | 35.48 | 9797216000 | 2363 | 2126 | 651593000 | 22.23 | 10865571000 | 1236473000 | 1249026 | 30105 | |
| 2010 | 59.4 | 635 | 578515000 | 35.48 | 9899274000 | 2346.8 | 2075 | 645420000 | 22.52 | 10926624000 | 1223935000 | 1257158 | 29933 | |
| 2011 | 59.4 | 685 | 530493000 | 35.65 | 10151225000 | 2337.8 | 2153 | 592478000 | 22.21 | 11228244000 | 1122971000 | 1241664 | 31359 | |
| 2012 | 59.4 | 730 | 589165000 | 35.65 | 10030824000 | 2216.9 | 2134 | 639718000 | 22.28 | 11158585000 | 1216529000 | 1246780 | 31842 | |
| 2013 | 59.4 | 730 | 583867000 | 35.65 | 9813548000 | 2188.7 | 2166 | 648817000 | 22.21 | 10812352000 | 1232684000 | 1243201 | 31300 | |
| 2014 | 59.4 | 730 | 450136000 | 35.65 | 9672501000 | 2196.2 | 2090 | 704362000 | 22.22 | 10823639000 | 1162593000 | 1259079 | 31985 | |
| 2015 | 65.4 | 730 | 456820000 | 35.65 | 10272709000 | 2141.1 | 2044 | 706841000 | 22.12 | 10940362000 | 1171723000 | 1267449 | 32489 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #https://www.reddit.com/r/econometrics/comments/5kgcq2/simple_ols_model_for_school_project_but_the/ | |
| require(reshape) | |
| require(ggplot2) | |
| require(pls) | |
| #load data | |
| setwd("~/RWD") | |
| metro <- read.csv("metro.csv", header=T, row.names=1) | |
| #OLS | |
| fullModel <- lm(M_3_passengers~., data=metro) | |
| partialModel <- lm(M_3_passengers~S_7_vehicles+S_8_passengers+T_11_passengers, data=metro) | |
| almostFullModel <- lm(M_3_passengers~.-T_11_passengers, data=metro) | |
| summary(fullModel) | |
| summary(partialModel) | |
| summary(almostFullModel) | |
| #correlation | |
| corMatrix <- round(cor(metro), 2) | |
| meltedCor <- melt(corMatrix) | |
| corPlot <- ggplot(data=meltedCor, aes(X1, X2, fill=value)) + | |
| geom_tile() + | |
| scale_fill_gradient2(low="red", mid="white", high="blue", limit = c(-1,1)) + | |
| theme(axis.text.x = element_text(angle = 90, hjust = 1)) + | |
| geom_text(aes(label = value), color = "black", size = 4) | |
| corPlot | |
| #PCA | |
| pcaMetro <- prcomp(metro) | |
| scree <- ggplot(data=as.data.frame(pcaMetro$sdev), aes(1:13, pcaMetro$sdev)) + | |
| geom_line() + | |
| geom_point() | |
| scree | |
| pcrMetro <- pcr(M_3_passengers~.-T_11_passengers, data=metro, ncomp=5) | |
| summary(pcrMetro) | |
| str(pcrMetro) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment