Alberknyis

Jags-Ymet-XmetMulti-Mrobust-Cameras-Run.R

Oct 19th, 2020 (edited)
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 4.07 KB | None | 0 0
  1. graphics.off() # This closes all of R's graphics windows.
  2. rm(list=ls())  # Careful! This clears all of R's memory!
  3.  
  4. library(ggplot2)
  5. library(ggpubr)
  6. library(ks)
  7. library(rjags)
  8. library(runjags)
  9.  
  10. # Set working directory if necessary
  11. # setwd(" ")
  12.  
  13. #na.omit for cleanup, there were some empty cells
  14. myData = na.omit(read.csv("camera_dataset.csv"))
  15. colnames(myData) = c("Model", "Release.date",   "Max.resolution",   "Low.resolution",   "Effective.pixels",    
  16.                      "Zoom.wide",   "Zoom.tele", "Normal.focus.range",  "Macro.focus.range",
  17.                      "Storage.included", "Weight.w.batteries", "Dimensions", "Price")
  18.  
  19. yName = "Price" ; xName = c("Release.date", "Max.resolution",   "Low.resolution",   "Effective.pixels",    
  20.                             "Zoom.wide",    "Zoom.tele", "Normal.focus.range",  "Macro.focus.range",
  21.                             "Storage.included", "Weight.w.batteries", "Dimensions")
  22. fileNameRoot = "Task9"
  23.  
  24. library(ggplot2)
  25. head(myData)
  26.  
  27. summary(myData)
  28. #graphics.off() # added to open plots in a new window. When plots are shown in the plots tab
  29. windows( width=480 , height=480) #they have low quality
  30. hist(myData$Price)
  31. #graphics.off() # added to open plots in a new window. When plots are shown in the plots tab
  32. windows( width=480 , height=480) #they have low quality
  33. plot(kde(myData$Price))
  34.  
  35.  
  36. # Scatter plots
  37. p1 <- ggplot(myData, aes(x=Release.date, y=Price)) +
  38.   geom_point()
  39.  
  40. p2 <- ggplot(myData, aes(x=Max.resolution, y=Price)) +
  41.   geom_point()
  42.  
  43. p3 <- ggplot(myData, aes(x=Low.resolution, y=Price)) +
  44.   geom_point()
  45.  
  46. p4 <- ggplot(myData, aes(x=Effective.pixels, y=Price)) +
  47.   geom_point()
  48.  
  49. p5 <- ggplot(myData, aes(x=Zoom.wide, y=Price)) +
  50.   geom_point()
  51.  
  52. p6 <- ggplot(myData, aes(x=Zoom.tele, y=Price)) +
  53.   geom_point()
  54.  
  55. p7 <- ggplot(myData, aes(x=Normal.focus.range, y=Price)) +
  56.   geom_point()
  57.  
  58. p8 <- ggplot(myData, aes(x=Macro.focus.range, y=Price)) +
  59.   geom_point()
  60.  
  61. p9 <- ggplot(myData, aes(x=Storage.included, y=Price)) +
  62.   geom_point()
  63.  
  64. p10 <- ggplot(myData, aes(x=Weight.w.batteries, y=Price)) +
  65.   geom_point()
  66.  
  67. p11 <- ggplot(myData, aes(x=Dimensions, y=Price)) +
  68.   geom_point()
  69.  
  70. figure <- ggarrange(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, nrow = 4, ncol = 3)
  71. #graphics.off() # added to open plots in a new window. When plots are shown in the plots tab
  72. windows( width=480 , height=480) #they have low quality
  73. figure
  74.  
  75.  
  76. numSavedSteps = 3500
  77. thinSteps = 10
  78. nChains = 4
  79.  
  80. graphFileType = "eps"
  81. #-------------------------------------------------------------------------------
  82. # Load the relevant model into R's working memory:
  83. source("Jags-Ymet-XmetMulti-Mrobust-Task9.R")
  84. #-------------------------------------------------------------------------------
  85. # Generate the MCMC chain:
  86. startTime = proc.time()
  87. xPred = c( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 )
  88.  
  89. mcmcCoda = genMCMC( data=myData , xName=xName , yName=yName ,
  90.                     numSavedSteps=numSavedSteps , thinSteps=thinSteps ,
  91.                     saveName=fileNameRoot , nChains = nChains , xPred = xPred )
  92. stopTime = proc.time()
  93. duration = stopTime - startTime
  94. show(duration)
  95.  
  96. # save.image(file='Task6Chains.RData')
  97. # load('Task6Chains.RData')
  98.  
  99.  
  100. #-------------------------------------------------------------------------------
  101. # Display diagnostics of chain, for specified parameters:
  102. parameterNames = varnames(mcmcCoda) # get all parameter names
  103. for ( parName in parameterNames ) {
  104.   diagMCMC( codaObject=mcmcCoda , parName=parName ,
  105.             saveName=fileNameRoot , saveType=graphFileType )
  106. }
  107. #graphics.off()
  108. #-------------------------------------------------------------------------------
  109. # Get summary statistics of chain:
  110.  
  111. summaryInfo = smryMCMC( mcmcCoda ,
  112.                         saveName=fileNameRoot  )
  113. show(summaryInfo)
  114. # Display posterior information:
  115. plotMCMC( mcmcCoda , data=myData , xName=xName , yName=yName ,
  116.           pairsPlot=TRUE , showCurve=FALSE ,
  117.           saveName=fileNameRoot , saveType=graphFileType )
  118. #-------------------------------------------------------------------------------
  119.  
Add Comment
Please, Sign In to add comment