Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.58 KB | None | 0 0
  1. #########################nulstiller miljø###################
  2.  
  3. rm(list=ls())
  4.  
  5. ###"########################### kalder libraries ##############################
  6.  
  7. pack<-c("car","sandwich","lmtest","RColorBrewer","mgcv","foreign","xtable"
  8. ,"AER","stargazer", "MASS","jpeg")
  9.  
  10. lapply(pack, require, character.only=T)
  11.  
  12. rm(pack)
  13.  
  14.  
  15.  
  16. ############################################# setting working path##########################
  17. data_path<-"C:/Users/Schweppes/Desktop/Econometrics"
  18. result_path<-"C:/Users/Schweppes/Desktop/Econometrics"
  19. graphs_path<-"C:/Users/Schweppes/Desktop/Econometrics"
  20.  
  21. ################# reading in adata########################################################
  22.  
  23.  
  24. setwd(data_path)
  25. dir()
  26.  
  27. load("adata")
  28. desc
  29.  
  30. ################################################################################################
  31.  
  32.  
  33. ###########################################################
  34. ### Tilføjer grid og tilhørende variable
  35. ###########################################################
  36.  
  37.  
  38. adata$tilex = ((adata$x - min(adata$x)) %/% (((max(adata$x) + 1) - min(adata$x)) / 10)) * 1000 + 1000
  39. adata$tiley = ((adata$y - min(adata$y)) %/% (((max(adata$y) + 1) - min(adata$y)) / 10)) * 10 + 10
  40. adata$grid = as.factor(adata$tilex + adata$tiley)
  41.  
  42. adata$in12 = adata$in1 + adata$in2
  43.  
  44. max(adata$x)
  45. min(adata$y)
  46. options(scipen=3)
  47. ###########################################################
  48.  
  49. mean(adata$price)
  50.  
  51. UndersogLog<-lm(log(price)~area
  52. +age
  53. +bathrooms
  54. +rebuild90
  55. +rebuild00
  56. +roof_cemen
  57. +roof_fiber
  58. +roof_board
  59. +roof_flat
  60. +roof_tile
  61. +railways
  62. +I(railways^2)
  63. +big_roads
  64. +I(big_roads^2)
  65. +in12
  66. +in3
  67. +in4
  68. +allgoods_WA_1000m
  69. +divers_shopservice_WA_1000m
  70. +grid
  71. ,adata)
  72.  
  73. Undersog<-lm(price~area
  74. +age
  75. +bathrooms
  76. +rebuild90
  77. +rebuild00
  78. +roof_cemen
  79. +roof_fiber
  80. +roof_board
  81. +roof_flat
  82. +roof_tile
  83. +railways
  84. +I(railways^2)
  85. +big_roads
  86. +I(big_roads^2)
  87. +in12
  88. +in3
  89. +in4
  90. +allgoods_WA_1000m
  91. +divers_shopservice_WA_1000m
  92. +grid
  93. ,adata)
  94. summary(Undersog)
  95.  
  96.  
  97. bptest(Undersog)
  98. coeftest(Undersog, vcov = vcovHC(Undersog, type = "HC"))
  99. #waldtest(Undersog,UndersogNy, vcov = vcovHC)
  100. resettest(Undersog)
  101.  
  102. par(mfrow=c(2,2))
  103. hist(UndersogLog$residuals)
  104. hist(Undersog$residuals)
  105. qqnorm(residuals(UndersogLog))
  106. qqline(residuals(UndersogLog))
  107. qqnorm(residuals(Undersog))
  108. qqline(residuals(Undersog))
  109.  
  110.  
  111. length(adata$price[adata$in1==1])
  112.  
  113. nytHus1 <- data.frame(area=110,rebuild80=0,rebuild90=0,rebuild00=1,roof_cemen=1
  114. ,roof_fiber=0,roof_board=0,roof_flat=0,roof_tile=0,grid="6050"
  115. ,in1=0,in12=0,in3=0,in4=1,allgoods_WA_1000m=1, railways = 1120, big_roads = 280
  116. ,divers_shopservice_WA_1000m=1,age=1990,bathrooms=2,floor=mean(adata$floor),date_num=mean(adata$date_num))
  117.  
  118. nytHus2 <- data.frame(area=110,rebuild80=0,rebuild90=0,rebuild00=1,roof_cemen=1
  119. ,roof_fiber=0,roof_board=0,roof_flat=0,roof_tile=0,grid="10050"
  120. ,in1=0,in12=0,in3=0,in4=1,allgoods_WA_1000m=1, railways = 1120, big_roads = 280
  121. ,divers_shopservice_WA_1000m=1,age=1990,bathrooms=2,floor=mean(adata$floor),date_num=mean(adata$date_num))
  122.  
  123. predict(Undersog,nytHus1,interval="predict")
  124. predict(Undersog,nytHus2,interval="predict")
  125.  
  126. exp((mean(Undersoglog$res^2))/2)*exp(predict(Undersoglog,nytHus1))
  127.  
  128. ####################Heatmap for samlet datasæt###########################
  129. par(mfrow=c(2,2))
  130. library(plot3D)
  131. neo = matrix(nrow = 10,ncol = 10)
  132. for (i in c(1:10)){
  133. for(l in c(1:10)){
  134. neo[i,l] = if (length(adata$price[adata$tilex == (l*1000) & adata$tiley == (110-10*(i))]) == 0) {NA} else {length(adata$price[adata$tilex == (l*1000) & adata$tiley == (110-10*(i))])}
  135. }
  136. }
  137. neo = t(apply(neo, 2, rev)) ###roterer heatmap
  138. image2D(neo, border = "black")
  139. title(main="densitet over samlet datasæt", col.main="red")
  140.  
  141.  
  142. ###############################################################################
  143.  
  144. ####################Heatmap for % af ældre mennesker###########################
  145. library(plot3D)
  146. neo = matrix(nrow = 10,ncol = 10)
  147. for (i in c(1:10)){
  148. for(l in c(1:10)){
  149. neo[i,l] = length(adata$price[adata$AgeMin61 == 1 & adata$tilex == (l*1000) & adata$tiley == (110-10*(i))]) / length(adata$price[adata$tilex == (l*1000) & adata$tiley == (110-10*(i))])
  150. }
  151. }
  152. neo = t(apply(neo, 2, rev)) ###roterer heatmap
  153. image2D(neo, border = "black")
  154. title(main="%-del af folk over 60", col.main="red")
  155. ###############################################################################
  156.  
  157. ####################Heatmap for gennemsnitlig pris###########################
  158. library(plot3D)
  159. neo = matrix(nrow = 10,ncol = 10)
  160. for (i in c(1:10)){
  161. for(l in c(1:10)){
  162. neo[i,l] = mean(adata$price[adata$tilex == (l*1000) & adata$tiley == (110-10*(i)) & length(adata$price[adata$tilex == (l*1000) & adata$tiley == (110-10*(i))]) >=5])
  163. }
  164. }
  165. neo = t(apply(neo, 2, rev)) ###roterer heatmap
  166. image2D(neo, border = "black")
  167. title(main="gnm-snit af pris", col.main="red")
  168. ###############################################################################
  169. v = c()
  170.  
  171.  
  172. for (i in c(1:10)){
  173. for(l in c(1:10)){
  174. v = c(v,neo[i,l])
  175. }
  176. }
  177. max(v,na.rm =TRUE)
  178. max(v,na.rm =TRUE)-min(v,na.rm =TRUE)
  179. min(v,na.rm =TRUE)
  180.  
  181. ####################Heatmap for gennemsnitlig liggetid###########################
  182. library(plot3D)
  183. neo = matrix(nrow = 10,ncol = 10)
  184. for (i in c(1:10)){
  185. for(l in c(1:10)){
  186. neo[i,l] = mean(adata$date_num[adata$tilex == (l*1000) & adata$tiley == (110-10*(i))])
  187. }
  188. }
  189. neo = t(apply(neo, 2, rev)) ###roterer heatmap
  190. image2D(neo, border = "black")
  191. title(main="gnm-snit af liggetid i dage", col.main="red")
  192. ###############################################################################
  193.  
  194.  
  195. #set.seed(1)
  196. #m <- cbind( A=sample(Undersog$residuals, 1000, replace=FALSE), B=sample(Undersog$residuals, 1000, replace=FALSE) )
  197. #colMeans(m)
  198.  
  199. #t.test(m[,1],m[,2], var.equal=TRUE, paired=FALSE)
  200.  
  201.  
  202. ###########Tester korrelation##############################
  203. testvif = vif(Undersog)
  204.  
  205. testvif^2
  206.  
  207. adatacor = subset(adata, select = c(price
  208. ,area
  209. ,age
  210. ,bathrooms
  211. ,rebuild90
  212. ,rebuild00
  213. ,roof_cemen
  214. ,roof_fiber
  215. ,roof_board
  216. ,roof_flat
  217. ,roof_tile
  218. ,railways
  219. #,I(railways^2)
  220. ,big_roads
  221. #,I(big_roads^2)
  222. ,in12
  223. ,in3
  224. ,in4
  225. ,allgoods_WA_1000m
  226. ,divers_shopservice_WA_1000m))
  227. res = cor(adatacor)
  228. round(res,2)
  229. ##############################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement