this month I started to gain my first experiences with real-life round-the-clock patient care. It's a wonderful mess really. At certain times, you feel jubilant for being able to take care of your patient efficiently, but at other times, you feel stressed that it does not exactly pan out like what you have hoped.
some interns choose to spend their supposed time-off inside the hospital: many of these instances because you want to get things done. i salute them for being extraordinary beyond the call of duty, literally.
anyway, it was just a week long and i have just barely scraped the entire pgh experience, but i felt really tired and fulfilled at the same time. i guess, i have to end this blogpost now, because postduty people would wisely use their time off sleeping rather than writing another word in a blog.
tutorials in python v R
UPDATED:
Here's the BioPython Tutorial and Cookbook --> BioPython
It's made by Jeff Chang and colleagues, and it seems to be a very useful reference for bioinformatics.
Actually, I have now found the Documentation website for Python 2.6 --> Python Docs
It probably has almost all basic functions defined in there. Of course, there's always StackOverflow...
------- ------- ------- ------- ------- ------- ------- ------- ------- ------- -------
For future reference, I'm keeping this Python tutorial URL --> Tutorials Point
Obviously I'm new to this. I'm keeping tabs on useful websites for Python learning.
I am not aware yet of a function that easily calls on the documentation from the Python GUI.
This makes my learning very arduous, a "trial-and-error" kind of thing.
#imstillhavingfunatleast
In contrast, R has "?" and "??" functions, which will search the appropriate documentation for that queried function. For example, I can type ?spplot, which will look up the term 'spplot' from the local library, i.e., files in the computer, or ??spplot, which will look up the term from the internet, i.e., from the CRAN website. #obviouslyimafan
If you happen to know how to search for Python functions more easily, do give me a heads up!
Currently, looking for the right function to write is a P.I.A. I'm not complaining though; I thank God that for some reason I'm having fun with this particular challenge.
senate election results
These data are sourced from the 16th Official Canvass Report from the Comelec Rappler Mirror Server. Retrieved May 23, 2013 4:30am. http://bit.ly/12AmEyO
you may also download the high-res dataviz from dropbox --> http://bit.ly/1a9KBPU
The following source code for making the heatmaps is working and functional in R v. 3.0.0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | #this code was tested in # R version 3.0.0 (2013-04-03) -- "Masked Marvel" # Copyright (C) 2013 The R Foundation for Statistical Computing # Platform: i386-w64-mingw32/i386 (32-bit) getwd() # this is your working directory # setwd("") # you can set your personal working directory here getwd() # ------------------------------------------------------------------------------------------------ # # LOAD VOTES DATASETS from COMELEC's 16th canvass report (OFFICIAL TALLY) # Accessed from http://www.rappler.com/nation/politics/elections-2013/features/rich-media/29126-official-tally-votes-2013-senatorial-race # Retrieved May 23, 2013 4:30am data1 <- "http://dl.dropboxusercontent.com/u/7911075/election%202013%20csv/namebyregion.csv" download.file(data1, destfile="./datasets/namebyregion.csv") data2 <- "http://dl.dropboxusercontent.com/u/7911075/election%202013%20csv/namebyprovince.csv" download.file(data2, destfile="./datasets/namebyprovince.csv") data3 <- "http://dl.dropboxusercontent.com/u/7911075/election%202013%20csv/provincetotalbyname.csv" download.file(data3, destfile="./datasets/provincetotalbyname.csv") data4 <- "http://dl.dropboxusercontent.com/u/7911075/election%202013%20csv/regiontotalbyname.csv" download.file(data4, destfile="./datasets/regiontotalbyname.csv") list.files(".") dateDownloaded <-date() dateDownloaded byregion <- read.csv("./datasets/namebyregion.csv", header=TRUE) str(byregion) byprovince <- read.csv("./datasets/namebyprovince.csv", header=TRUE) str(byprovince) tprovince <- read.csv("./datasets/provincetotalbyname.csv", header=TRUE) str(tprovince) tregion <- read.csv("./datasets/regiontotalbyname.csv", header=TRUE) str(tregion) # ------------------------------------------------------------------------------------------------ # # LOAD COLOR PALETTES library(RColorBrewer) cols4v1 <- brewer.pal(5, "YlGnBu") pal4v1 <- colorRampPalette(cols4v1) # <- dark blue cyan white gradient cols4v2 <- brewer.pal(5, "YlOrBr") pal4v2 <- colorRampPalette(cols4v2) # <- brown orange white gradient cols4v3 <- brewer.pal(5, "PuRd") pal4v3 <- colorRampPalette(cols4v3) # <- purple pink white gradient cols4v4 <- brewer.pal(5, "RdPu") pal4v4 <- colorRampPalette(cols4v4) # <- violet pink white gradient cols8 <- brewer.pal(8, "Set3") pal8 <- colorRampPalette(cols8) cols12 <- brewer.pal(12, "Set3") pal12 <- colorRampPalette(cols12) # ------------------------------------------------------------------------------------------------ # # DRAW HEATMAPS FOR REGION row.names(byregion) <- byregion$X byregion <- byregion[,2:19] byregion_matrix <- data.matrix(byregion) regions_heatmap1 <- heatmap(byregion_matrix, Rowv=NA, Colv=NA, col = cm.colors(256), scale="column", margins=c(5,10)) # <-- pink, blue regions_heatmap2 <- heatmap(byregion_matrix, Rowv=NA, Colv=NA, col = heat.colors(256), scale="column", margins=c(5,10)) # <-- too much red regions_heatmap3 <- heatmap(byregion_matrix, Rowv=NA, Colv=NA, col = pal4v2(5), scale="column", margins=c(5,10)) # <-- yellow brown gradient regions_heatmap4 <- heatmap(byregion_matrix, Rowv=NA, Colv=NA, col = pal4v1(5), scale="column", margins=c(5,10)) # <-- yellow blue gradient regions_heatmap5 <- heatmap(byregion_matrix, Rowv=NA, Colv=NA, col = pal12(5), scale="column", margins=c(5,10)) # <- messy looking regions_heatmap6 <- heatmap(byregion_matrix, Rowv=NA, Colv=NA, col = pal4v4(5), scale="column", margins=c(5,10)) # <-- purle pink gradient # dev.copy2pdf(file="./output/votesperregion.pdf", height = 8, width = 11) # ------------------------------------------------------------------------------------------------ # # DRAW HEATMAPS PER PROVINCE str(byprovince) names(byprovince) # REGION 1 ------------------------------------------------------------------------------------------------ # for example, we take the provinces in region1 provincesregion1 <- byprovince[,33:37] provincesregion1$X <- byprovince$X str(provincesregion1) row.names(provincesregion1) <- provincesregion1$X provincesregion1 <- provincesregion1[,1:5] provincesregion1_matrix <- data.matrix(provincesregion1) #draw heatmap for provinces in region1 region1_heatmap <- heatmap(provincesregion1_matrix, Rowv=NA, Colv=NA, col = pal4v1(5), scale="column", margins=c(5,10)) # <-- yellow blue gradient dev.copy2pdf(file="./output/votesregion1.pdf", height = 8, width = 11) # REGION 2 ------------------------------------------------------------------------------------------------ # for example, we take the provinces in region2 provincesregion2 <- byprovince[,38:43] provincesregion2$X <- byprovince$X str(provincesregion2) row.names(provincesregion2) <- provincesregion2$X provincesregion2 <- provincesregion2[,1:6] provincesregion2_matrix <- data.matrix(provincesregion2) #draw heatmap for provinces in region2 region2_heatmap <- heatmap(provincesregion2_matrix, Rowv=NA, Colv=NA, col = pal4v1(5), scale="column", margins=c(5,10)) # <-- yellow blue gradient # dev.copy2pdf(file="./output/votesregion2.pdf", height = 8, width = 11) # REGION 3 ------------------------------------------------------------------------------------------------ # for example, we take the provinces in region3 provincesregion3 <- byprovince[,44:51] provincesregion3$X <- byprovince$X str(provincesregion3) row.names(provincesregion3) <- provincesregion3$X provincesregion3 <- provincesregion3[,1:8] provincesregion3_matrix <- data.matrix(provincesregion3) #draw heatmap for provinces in region3 region3_heatmap <- heatmap(provincesregion3_matrix, Rowv=NA, Colv=NA, col = pal4v1(5), scale="column", margins=c(5,10)) # <-- yellow blue gradient # dev.copy2pdf(file="./output/votesregion3.pdf", height = 8, width = 11) # REGION 4A ------------------------------------------------------------------------------------------------ # for example, we take the provinces in region4a provincesregion4a <- byprovince[,52:57] provincesregion4a$X <- byprovince$X str(provincesregion4a) row.names(provincesregion4a) <- provincesregion4a$X provincesregion4a <- provincesregion4a[,1:6] provincesregion4a_matrix <- data.matrix(provincesregion4a) #draw heatmap for provinces in region4a region4a_heatmap <- heatmap(provincesregion4a_matrix, Rowv=NA, Colv=NA, col = pal4v1(5), scale="column", margins=c(5,10)) # <-- yellow blue gradient # dev.copy2pdf(file="./output/votesregion4a.pdf", height = 8, width = 11) # REGION4B ------------------------------------------------------------------------------------------------ # for example, we take the provinces in region4b provincesregion4b <- byprovince[,58:63] provincesregion4b$X <- byprovince$X str(provincesregion4b) row.names(provincesregion4b) <- provincesregion4b$X provincesregion4b <- provincesregion4b[,1:6] provincesregion4b_matrix <- data.matrix(provincesregion4b) #draw heatmap for provinces in region4b region4b_heatmap <- heatmap(provincesregion4b_matrix, Rowv=NA, Colv=NA, col = pal4v1(5), scale="column", margins=c(5,10)) # <-- yellow blue gradient # dev.copy2pdf(file="./output/votesregion4b.pdf", height = 8, width = 11) # REGION5 ------------------------------------------------------------------------------------------------ # for example, we take the provinces in region5 provincesregion5 <- byprovince[,64:70] provincesregion5$X <- byprovince$X str(provincesregion5) row.names(provincesregion5) <- provincesregion5$X provincesregion5 <- provincesregion5[,1:7] provincesregion5_matrix <- data.matrix(provincesregion5) #draw heatmap for provinces in region5 region5_heatmap <- heatmap(provincesregion5_matrix, Rowv=NA, Colv=NA, col = pal4v1(5), scale="column", margins=c(5,10)) # <-- yellow blue gradient # dev.copy2pdf(file="./output/votesregion5.pdf", height = 8, width = 11) # REGION6 ------------------------------------------------------------------------------------------------ # for example, we take the provinces in region6 provincesregion6 <- byprovince[,71:79] provincesregion6$X <- byprovince$X str(provincesregion6) row.names(provincesregion6) <- provincesregion6$X provincesregion6 <- provincesregion6[,1:9] provincesregion6_matrix <- data.matrix(provincesregion6) #draw heatmap for provinces in region6 region6_heatmap <- heatmap(provincesregion6_matrix, Rowv=NA, Colv=NA, col = pal4v1(5), scale="column", margins=c(5,10)) # <-- yellow blue gradient # dev.copy2pdf(file="./output/votesregion6.pdf", height = 8, width = 11) # REGION7 ------------------------------------------------------------------------------------------------ # for example, we take the provinces in region7 provincesregion7 <- byprovince[,80:86] provincesregion7$X <- byprovince$X str(provincesregion7) row.names(provincesregion7) <- provincesregion7$X provincesregion7 <- provincesregion7[,1:7] provincesregion7_matrix <- data.matrix(provincesregion7) #draw heatmap for provinces in region7 region7_heatmap <- heatmap(provincesregion7_matrix, Rowv=NA, Colv=NA, col = pal4v1(5), scale="column", margins=c(5,10)) # <-- yellow blue gradient # dev.copy2pdf(file="./output/votesregion7.pdf", height = 8, width = 11) # REGION8 ------------------------------------------------------------------------------------------------ # for example, we take the provinces in region8 provincesregion8 <- byprovince[,87:93] provincesregion8$X <- byprovince$X str(provincesregion8) row.names(provincesregion8) <- provincesregion8$X provincesregion8 <- provincesregion8[,1:7] provincesregion8_matrix <- data.matrix(provincesregion8) #draw heatmap for provinces in region8 region8_heatmap <- heatmap(provincesregion8_matrix, Rowv=NA, Colv=NA, col = pal4v1(5), scale="column", margins=c(5,10)) # <-- yellow blue gradient # dev.copy2pdf(file="./output/votesregion8.pdf", height = 8, width = 11) # REGION9 ------------------------------------------------------------------------------------------------ # for example, we take the provinces in region9 provincesregion9 <- byprovince[,94:98] provincesregion9$X <- byprovince$X str(provincesregion9) row.names(provincesregion9) <- provincesregion9$X provincesregion9 <- provincesregion9[,1:5] provincesregion9_matrix <- data.matrix(provincesregion9) #draw heatmap for provinces in region9 region9_heatmap <- heatmap(provincesregion9_matrix, Rowv=NA, Colv=NA, col = pal4v1(5), scale="column", margins=c(5,10)) # <-- yellow blue gradient # dev.copy2pdf(file="./output/votesregion9.pdf", height = 8, width = 11) # REGION10 ------------------------------------------------------------------------------------------------ # for example, we take the provinces in region10 provincesregion10 <- byprovince[,99:106] provincesregion10$X <- byprovince$X str(provincesregion10) row.names(provincesregion10) <- provincesregion10$X provincesregion10 <- provincesregion10[,1:7] provincesregion10_matrix <- data.matrix(provincesregion10) #draw heatmap for provinces in region10 region10_heatmap <- heatmap(provincesregion10_matrix, Rowv=NA, Colv=NA, col = pal4v1(5), scale="column", margins=c(5,10)) # <-- yellow blue gradient # dev.copy2pdf(file="./output/votesregion10.pdf", height = 8, width = 11) # REGION11 ------------------------------------------------------------------------------------------------ # for example, we take the provinces in region11 provincesregion11 <- byprovince[,107:112] provincesregion11$X <- byprovince$X str(provincesregion11) row.names(provincesregion11) <- provincesregion11$X provincesregion11 <- provincesregion11[,1:6] provincesregion11_matrix <- data.matrix(provincesregion11) #draw heatmap for provinces in region11 region11_heatmap <- heatmap(provincesregion11_matrix, Rowv=NA, Colv=NA, col = pal4v1(5), scale="column", margins=c(5,10)) # <-- yellow blue gradient # dev.copy2pdf(file="./output/votesregion11.pdf", height = 8, width = 11) # REGION12 ------------------------------------------------------------------------------------------------ # for example, we take the provinces in region12 provincesregion12 <- byprovince[,113:117] provincesregion12$X <- byprovince$X str(provincesregion12) row.names(provincesregion12) <- provincesregion12$X provincesregion12 <- provincesregion12[,1:5] provincesregion12_matrix <- data.matrix(provincesregion12) #draw heatmap for provinces in region12 region12_heatmap <- heatmap(provincesregion12_matrix, Rowv=NA, Colv=NA, col = pal4v1(5), scale="column", margins=c(5,10)) # <-- yellow blue gradient # dev.copy2pdf(file="./output/votesregion12.pdf", height = 8, width = 11) # REGION13 CARAGA ------------------------------------------------------------------------------------------------ # for example, we take the provinces in region13 provincesregion13 <- byprovince[,10:15] provincesregion13$X <- byprovince$X str(provincesregion13) row.names(provincesregion13) <- provincesregion13$X provincesregion13 <- provincesregion13[,1:6] provincesregion13_matrix <- data.matrix(provincesregion13) #draw heatmap for provinces in region13 region13_heatmap <- heatmap(provincesregion13_matrix, Rowv=NA, Colv=NA, col = pal4v1(5), scale="column", margins=c(5,10)) # <-- yellow blue gradient # dev.copy2pdf(file="./output/votesregion13.pdf", height = 8, width = 11) # NCR ------------------------------------------------------------------------------------------------ # for example, we take the provinces in ncr provincesncr <- byprovince[,16:32] provincesncr$X <- byprovince$X str(provincesncr) row.names(provincesncr) <- provincesncr$X provincesncr <- provincesncr[,1:17] provincesncr_matrix <- data.matrix(provincesncr) #draw heatmap for provinces in ncr ncr_heatmap <- heatmap(provincesncr_matrix, Rowv=NA, Colv=NA, col = pal4v1(5), scale="column", margins=c(5,10)) # <-- yellow blue gradient # dev.copy2pdf(file="./output/votesncr.pdf", height = 8, width = 11) # CAR ------------------------------------------------------------------------------------------------ # for example, we take the provinces in car provincescar <- byprovince[,2:9] provincescar$X <- byprovince$X str(provincescar) row.names(provincescar) <- provincescar$X provincescar <- provincescar[,1:8] provincescar_matrix <- data.matrix(provincescar) #draw heatmap for provinces in car car_heatmap <- heatmap(provincescar_matrix, Rowv=NA, Colv=NA, col = pal4v1(5), scale="column", margins=c(5,10)) # <-- yellow blue gradient # dev.copy2pdf(file="./output/votescar.pdf", height = 8, width = 11) # ARMM ------------------------------------------------------------------------------------------------ # for example, we take the provinces in armm provincesarmm <- byprovince[,118:123] provincesarmm$X <- byprovince$X str(provincesarmm) row.names(provincesarmm) <- provincesarmm$X provincesarmm <- provincesarmm[,1:6] provincesarmm_matrix <- data.matrix(provincesarmm) #draw heatmap for provinces in armm armm_heatmap <- heatmap(provincesarmm_matrix, Rowv=NA, Colv=NA, col = pal4v1(5), scale="column", margins=c(5,10)) # <-- yellow blue gradient # dev.copy2pdf(file="./output/votesarmm.pdf", height = 8, width = 11) # OAV OVERSEAS ABSENTEE VOTING COUNTRIES ------------------------------------------------------------------------------------------------ # for example, we take the provinces in oav provincesoav <- byprovince[,124:146] provincesoav$X <- byprovince$X str(provincesoav) row.names(provincesoav) <- provincesoav$X provincesoav <- provincesoav[,1:23] provincesoav_matrix <- data.matrix(provincesoav) #draw heatmap for provinces in oav oav_heatmap <- heatmap(provincesoav_matrix, Rowv=NA, Colv=NA, col = pal4v1(5), scale="column", margins=c(5,10)) # <-- yellow blue gradient # dev.copy2pdf(file="./output/votesoav.pdf", height = 8, width = 11) # # --------------------------- # END OF CODE |
dataviz on antenatal care
in a previous post, i showed you how to make choropleth maps, particularly both world and Philippine maps, using R (see previous post). In this case we used the data we collected from the websites of the World Health Organization and the Philippine Department of Health.

ironman3 v startrek2
![]() |
this news report was brought to you by xkcd |
- i hate doing reviews; one can argue that films don't need them.
- reviews exist because of movies, not vice versa.
- don't get me wrong; star trek is pretty solid work from the reliable jj abrams.
- it had a tight script, crazy visuals, character buildup, and some bromance on the side.
- that being said, i agonized for 2 hours inside the cinema.
- i was hoping to find salvation for startrek2 after much anticipation.
- i wanted that one break-out moment that can topple iron man 3.
- there was none.
- to be fair, my pre-expectations were quite high for star trek, and a bit low for iron man.
- there was a very high chance that it will fail my expectations.
- as a trekkie, it pains me to say that ironman3 has edged out startrek2 in the emotions department.
- where did my good ol' jj abrams go?
- ironman3 felt, surprisingly, more human.
- you can do the review on your own.
- think about 3 themes:
- infallibility: which felt more vulnerable?
- sense of threat: what was the main goal of the antagonist? was it killing or was it just escaping?
- consequences: do you really think that the protagonist will die?
- having said that, startrek is best seen on 3D and with fellow trekkies.
- live long and prosper, b*tches.
choropleth maps in R
below is the source code for making the choropleths for the infographic about newborns. you can run the following code in R. By the way, I'm using version 3.0.0; some library packages may have a different syntax and output in older versions of R.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | getwd() # this is your working directory on your computer # DOWNLOAD THE FOLLOWING DATA FROM THE INTERNET provinces <- "http://dl.dropboxusercontent.com/u/7911075/csv%20newborn/provinces.csv" download.file(provinces, destfile="./provinces.csv") phattended <- "http://dl.dropboxusercontent.com/u/7911075/csv%20newborn/dataPH2003-2008attended.csv" download.file(phattended, destfile="./dataPH2003-2008attended.csv") phbirthshome <- "http://dl.dropboxusercontent.com/u/7911075/csv%20newborn/dataPH2003-2008facility.csv" download.file(phbirthshome, destfile="./dataPH2003-2008facility.csv") countries <- "http://dl.dropboxusercontent.com/u/7911075/csv%20newborn/datacountries2005-2011attended.csv" download.file(countries, destfile="./datacountries2005-2011attended.csv") # this is a large file from the gadm website, might take a while to finish downloading phil1 <- "http://gadm.org/data/rda/PHL_adm1.RData" download.file(phil1, destfile="./phil1.RData") list.files(".") dateDownloaded <-date() dateDownloaded # ------------------------------------------------------------------------------------------------ # # LOAD GEO MAPS library(sp) con <- "./phil1.RData" print(load(con)) close(con) str(gadm, max.level=2) # phil1.RData has 82 observations of 16 variables names(gadm) # [1] "ID_0" "ISO" "NAME_0" "ID_1" "NAME_1" # [6] "VARNAME_1" "NL_NAME_1" "HASC_1" "CC_1" "TYPE_1" # [11] "ENGTYPE_1" "VALIDFR_1" "VALIDTO_1" "REMARKS_1" "Shape_Leng" # [16] "Shape_Area" # ------------------------------------------------------------------------------------------------ # # LOAD PROVINCES DATASET provinces <- read.csv("./provinces.csv") str(provinces) # ------------------------------------------------------------------------------------------------ # # LOAD DATA BIRTHS ATTENDED BY SKILLED PERSONNEL phattended <- read.csv("./dataPH2003-2008attended.csv", header=TRUE) str(phattended) #calculate percent births with skilled attendants, i.e., doctors, nurses, and midwives only phattended$skilled2008percent <- (phattended$X2008doctorspercent + phattended$X2008nursespercent + phattended$X2008midwivespercent) phattended$skilled2007percent <- (phattended$X2007doctorspercent + phattended$X2007nursespercent + phattended$X2007midwivespercent) phattended$skilled2006percent <- (phattended$X2006doctorspercent + phattended$X2006nursespercent + phattended$X2006midwivespercent) phattended$skilled2005percent <- (phattended$X2005doctorspercent + phattended$X2005nursespercent + phattended$X2005midwivespercent) phattended$skilled2004percent <- (phattended$X2004doctorspercent + phattended$X2004nursespercent + phattended$X2004midwivespercent) phattended$skilled2003percent <- (phattended$X2003doctorspercent + phattended$X2003nursespercent + phattended$X2003midwivespercent) #example: barplot(phattended$X2008midwivespercent, col=unique(phattended$REGION), xlab="", ylab="", cex.axis=0.8) #multiply by 100 phattended$skilled2008percent <- phattended$skilled2008percent*100 phattended$skilled2007percent <- phattended$skilled2007percent*100 phattended$skilled2006percent <- phattended$skilled2006percent*100 phattended$skilled2005percent <- phattended$skilled2005percent*100 phattended$skilled2004percent <- phattended$skilled2004percent*100 phattended$skilled2003percent <- phattended$skilled2003percent*100 # ------------------------------------------------------------------------------------------------ # # LOAD DATA PHL HOMEBIRTHS, NORMAL SPONTANEOUS DELIVERY phbirthshome <- read.csv("./dataPH2003-2008facility.csv", header=TRUE) str(phbirthshome) names(phbirthshome) # ------------------------------------------------------------------------------------------------ # # FIRST MERGE: INNER JOIN FOR DATASETS OF % HOME BIRTHS & % ATTENDED BY SKILLED PERSONNEL phbirthdata <- merge(phattended, phbirthshome, by="REGION") str(phbirthdata) # ------------------------------------------------------------------------------------------------ # # SECOND MERGE: INNER JOIN TO PROVINCE DATASET provattended1 <- merge(provinces, phbirthdata, by="REGION") str(provattended1) # write.csv(provattended1, file="provattended1.csv") # SORT MERGED DATASET TO ARRANGE ACCDG TO GADM MAP's ORDER provattended1.sorted <- provattended1[order(provattended1$PROVINCEORDER) , ] # write.csv(provattended1.sorted, file="provattended1.sorted.csv") # ------------------------------------------------------------------------------------------------------------ # # NOW WE CAN MERGE THE COMBINED DATASET TO THE GADM MAP gadm$region <- as.factor(provattended1.sorted$REGION) gadm$capital <- as.factor(provattended1.sorted$CAPITAL) gadm$popln2010 <- as.numeric(provattended1.sorted$POPLN2010) gadm$landarea <- as.numeric(provattended1.sorted$LANDAREA.km2) gadm$popdense <- as.numeric(provattended1.sorted$POPDENS2010) gadm$livebirths2008 <- as.numeric(provattended1.sorted$X2008total) gadm$skilled2008 <- as.numeric(provattended1.sorted$skilled2008percent) gadm$homebirths2008 <- as.numeric(provattended1.sorted$X2008HOMEpercent) gadm$hospbirths2008 <- as.numeric(provattended1.sorted$X2008HOSPpercent) library(Hmisc) library(RColorBrewer) cols4v1 <- brewer.pal(5, "YlGnBu") pal4v1 <- colorRampPalette(cols4v1) # <- dark blue cyan white gradient cols4v2 <- brewer.pal(5, "YlOrBr") pal4v2 <- colorRampPalette(cols4v2) # <- brown orange white gradient cols4v3 <- brewer.pal(5, "PuRd") pal4v3 <- colorRampPalette(cols4v3) # <- purple pink white gradient cols8 <- brewer.pal(8, "Set3") pal8 <- colorRampPalette(cols8) cols12 <- brewer.pal(12, "Set3") pal12 <- colorRampPalette(cols12) str(gadm$region) str(gadm$capital) str(gadm$popln2010) str(gadm$landarea) str(gadm$popdense) str(gadm$livebirths2008) str(gadm$skilled2008) str(gadm$homebirths2008) # -------------------------------------------------------------------- # PARSE REGIONS INTO LOW-AVERAGE-HIGH gadm$g4area <- cut2(gadm$landarea, g = 4) table(gadm$g4area) gadm$g4popln <- cut2(gadm$popln2010, g = 4) table(gadm$g4popln) gadm$g4popdense <- cut2(gadm$popdense, g = 4) table(gadm$g4popdense) gadm$g4livebirths2008 <-cut2(gadm$livebirths2008, g = 4) table(gadm$g4livebirths2008) gadm$g4skilled2008 <-cut2(gadm$skilled2008, g=4) table(gadm$g4skilled2008) gadm$g4homebirths2008 <- cut2(gadm$homebirths2008, g=4) table(gadm$g4homebirths2008) gadm$g4hospbirths2008 <- cut2(gadm$hospbirths2008, g=4) table(gadm$g4hospbirths2008) gadm$g8popln <- cut2(gadm$popln2010, g = 8) table(gadm$g8popln) gadm$g8popdense <- cut2(gadm$popdense, g = 8) table(gadm$g8popdense) colg4skilled4v1 = pal4v1(length(levels(gadm$g4skilled2008))) spplot(gadm, "g4skilled2008", col.regions=colg4skilled4v1, main="% Births with Skilled Attendants, 2008 by Region") # dev.copy2pdf(file="PH percent births attended 2008.pdf", height =11, width = 8) colg4home4v2 = pal4v2(length(levels(gadm$g4homebirths2008))) spplot(gadm, "g4homebirths2008", col.regions=colg4home4v2, main="% Births at Home, Normal Spontaneous Delivery, 2008 by Region") # dev.copy2pdf(file="PH percent births at home 2008.pdf", height =11, width = 8) colg4hosp4v3 = pal4v3(length(levels(gadm$g4hospbirths2008))) spplot(gadm, "g4hospbirths2008", col.regions=colg4hosp4v3, main="% Births at Hospital, Normal Spontaneous Delivery, 2008 by Region") # dev.copy2pdf(file="PH percent births at hospital 2008.pdf", height =11, width = 8) colpopln4v1 = pal4v1(length(levels(gadm$g4popln))) # <- dark blue cyan white gradient colpopln4v2 = pal4v2(length(levels(gadm$g4popln))) # <- brown orange white gradient colpopln4v3 = pal4v3(length(levels(gadm$g4popln))) # <- purple pink white gradient # spplot(gadm, "g4popln", col.regions=colpopln4v1, main="Population by Province") colregions = pal12(length(levels(gadm$region))) # spplot(gadm, "region", col.regions=colregions, main="Regions in the Philippines") # dev.copy2pdf(file="PH regions.pdf", height =11, width = 8) colpopln8 = pal8(length(levels(gadm$g8popln))) # spplot(gadm, "g8popln", col.regions=colpopln8, main="Population by Province") # dev.copy2pdf(file="PH population per province.pdf", height =11, width = 8) colpopdense8 = pal8(length(levels(gadm$g8popdense))) # spplot(gadm, "g8popdense", col.regions=colpopdense8, main="Population Density by Province") # dev.copy2pdf(file="PH population density per province.pdf", height =11, width = 8) collivebirth4v2 = pal4v2(length(levels(gadm$g4livebirths2008))) # spplot(gadm, "g4livebirths2008", col.regions=collivebirth4v2, main="Live Births 2008") # dev.copy2pdf(file="live births 2008.pdf", height =11, width = 8) # ------------------------------------------------------------------------------------------ # # MAKE WORLD MAP countries <- read.csv("./datacountries2005-2011attended.csv", header=TRUE) str(countries) countries$skilled100 <- countries$birthsattendedpercent*100 countries$g8skilled100 <- cut2(countries$skilled100, g = 8) table(countries$g8skilled100) library(Hmisc) library(RColorBrewer) cols4v1 <- brewer.pal(5, "YlGnBu") pal4v1 <- colorRampPalette(cols4v1) # <- dark blue cyan white gradient cols4v2 <- brewer.pal(5, "YlOrBr") pal4v2 <- colorRampPalette(cols4v2) # <- brown orange white gradient cols4v3 <- brewer.pal(5, "PuRd") pal4v3 <- colorRampPalette(cols4v3) # <- purple pink white gradient cols8 <- brewer.pal(8, "Set3") pal8 <- colorRampPalette(cols8) cols8v2 <- brewer.pal(8, "YlOrRd") pal8v2 <- colorRampPalette(cols8v2) cols8v3 <- brewer.pal(8, "YlGnBu") pal8v3 <- colorRampPalette(cols8v3) cols8v4 <- brewer.pal(8, "RdPu") pal8v4 <- colorRampPalette(cols8v4) cols12 <- brewer.pal(12, "Set3") pal12 <- colorRampPalette(cols12) library(rworldmap) mapattended <- joinCountryData2Map(countries, joinCode = "ISO3", nameJoinColumn = "ISO3V10") str(mapattended, max.level=2) par(mai=c(0,0,0.2,0),xaxs="i",yaxs="i") mapParams <- mapCountryData(mapattended, nameColumnToPlot="skilled100", catMethod = "pretty", numCats = 8, colourPalette = cols8v3, mapTitle="", addLegend=FALSE) do.call( addMapLegend, c(mapParams, legendWidth=0.5, legendMar = 2, legendLabels="all", legendIntervals="data")) # dev.copy2pdf(file="countries births skilled attendants 2005-2011 v2.pdf", height =8, width = 11) |
my osce recipe
masked marvel is out
R version 3.0.0, codename masked marvel, is now ready for download! (link)
See the newest iteration of the open-source community's beloved environment for statistics computing and graphics. (homepage) The stable version was officially released on April 3, 2013.
I've been waiting for this for the past few months. I've been number-crunching during January to March using version2.15, and I was hesitant to download v2.16, because I knew for a fact that v3.0 is due for release this year. I had difficulty using certain packages, such as randomForest, because they won't work on v2.15.
So here now my patience has paid off, and such a great timing this was: school year's almost over, and I can install the software and upgrade packages to my heart's content. I have all the time that I need.
Professor Peter Dalgaard of the Copenhagen Business School precisely wrote, in behalf of the R core team, in his letter to the community mailing list saying that R v3.0.0 is not so much about adding a lot of new features from v2.0 iterations, but rather it was decided that the codebase has "developed to a new level of maturity" that warrants a major release.
I've been waiting for this for the past few months. I've been number-crunching during January to March using version2.15, and I was hesitant to download v2.16, because I knew for a fact that v3.0 is due for release this year. I had difficulty using certain packages, such as randomForest, because they won't work on v2.15.
So here now my patience has paid off, and such a great timing this was: school year's almost over, and I can install the software and upgrade packages to my heart's content. I have all the time that I need.
Professor Peter Dalgaard of the Copenhagen Business School precisely wrote, in behalf of the R core team, in his letter to the community mailing list saying that R v3.0.0 is not so much about adding a lot of new features from v2.0 iterations, but rather it was decided that the codebase has "developed to a new level of maturity" that warrants a major release.
Version 3.0.0, as of this writing, contains only really major new feature: The inclusion of long vectors... More changes are likely to make it into the final release, but the main reason for having it as a new major release is that R over the last 8.5 years has reached a new level: we now have 64 bit support on all platforms, support for parallel processing, the Matrix package, and much more.Thank you R core team! Happy data crunching.
R code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 | getwd() # setwd("C:/Users/aseandi/My Library/COURSERA/Passion-Driven Statistics/datasets") getwd() # fileurlcsv <- "http://spark-public.s3.amazonaws.com/pdstatistics/data_sets/nesarc_pds.csv" # download.file(fileurlcsv, destfile="./nesarc_pds.csv") # list.files(".") # dateDownloaded <-date() # dateDownloaded nesarc <- read.csv("./nesarc_pds.csv") str(nesarc) # summary(nesarc) # sapply(nesarc[1, ], class) # sum(is.na(nesarc)) # table(is.na(nesarc)) library(Hmisc) library(RColorBrewer) library(reshape2) library(stringr) library(plyr) # # -------------------------------------------------------------------------------------------------------- # # # DEFINING THE VARIABLES UNDER STUDY # # # # study variables: Sex, average daily quantity of alcohol consumed and cigarettes smoked in past 12 months, and use of sedatives, tranquilizers, cannabis, opioids, amphetamines, cocaine, heroine, hallucinogens, and inhalants # codes: Sex, s2aq8b, s3aq3c1, S3bq1a1, S3bq1a2, S3bq1a3, S3bq1a4, S3bq1a5, S3bq1a6, S3bq1a7, S3bq1a8, S3bq1a9a #number of alcohol consumed in past 12 months table(is.na(nesarc$S2AQ8B)) table(nesarc$S2AQ8B) sum(table(nesarc$S2AQ8B)) #usual quantity of cigarettes smoked table(is.na(nesarc$S3AQ3C1)) table(nesarc$S3AQ3C1) sum(table(nesarc$S3AQ3C1)) # # -------------------------------------------------------------------------------------------------------- # without DATA MUNGING, it becomes weird, since binomials are coded differently [as 1 or 2], and NAs were assigned a numeric value [9] # here we are also only using LINEAR REGRESSION modeling, not accounting for the binomial distribution of some of these variables lmNoAdjust <- lm(MAJORDEPLIFE ~ SEX+ S2AQ8B+ S3AQ3C1+ S3BQ1A1+ S3BQ1A2+ S3BQ1A3+ S3BQ1A4+ S3BQ1A5+ S3BQ1A6+ S3BQ1A7+ S3BQ1A8+ S3BQ1A9A, data=nesarc) summary(lmNoAdjust) # Coefficients: # Estimate Std. Error t value Pr(>|t|) # (Intercept) 0.0347409 0.0223817 1.552 0.120639 # SEX 0.1533657 0.0072438 21.172 < 2e-16 *** # S2AQ8B 0.0001574 0.0003837 0.410 0.681605 # S3AQ3C1 0.0010271 0.0002495 4.117 3.86e-05 *** # S3BQ1A1 -0.0788230 0.0155627 -5.065 4.14e-07 *** # S3BQ1A2 -0.0113460 0.0163401 -0.694 0.487463 # S3BQ1A3 -0.0418769 0.0148434 -2.821 0.004791 ** # S3BQ1A4 -0.0030821 0.0137318 -0.224 0.822409 # S3BQ1A5 -0.0815162 0.0080903 -10.076 < 2e-16 *** # S3BQ1A6 -0.0115252 0.0133963 -0.860 0.389627 # S3BQ1A7 -0.0105396 0.0134567 -0.783 0.433512 # S3BQ1A8 0.0517945 0.0150253 3.447 0.000568 *** # S3BQ1A9A 0.1440555 0.0179928 8.006 1.28e-15 *** # # -------------------------------------------------------------------------------------------------------- # # # CREATE NEW DATAFRAME WITH FEWER VARIABLES # # # mddold <- data.frame(nesarc$MAJORDEPLIFE, nesarc$SEX, nesarc$S3AQ3B1, nesarc$S3AQ3C1, nesarc$S2AQ8A, nesarc$S2AQ8B, nesarc$S3BQ1A1, nesarc$S3BQ1A2, nesarc$S3BQ1A3, nesarc$S3BQ1A4, nesarc$S3BQ1A5, nesarc$S3BQ1A6, nesarc$S3BQ1A7, nesarc$S3BQ1A8, nesarc$S3BQ1A9A) str(mddold) names(mddold) <- c("mdd", "sex", "smokefreq", "smoke", "drinkfreq", "alcohol", "sedatives", "tranquilizers", "opioids", "amphetamines", "cannabis", "cocaine", "hallucinogens", "inhalants", "heroine") str(mddold) # # -------------------------------------------------------------------------------------------------------- # # # DATA MUNGING # # # # for the following variables: "sex", "sedatives", "tranquilizers", "cannabis", "opioids", "amphetamines", "cocaine", "heroine", "hallucinogens", "inhalants" # change the binomial setting [1,2] --> [1,0] # change [9] to [NA] # # # DATA MUNGING # # # # for the following variables: "alcohol", "smoke", "smokefreq", "drinkfreq" # change [99] for smoke quantity, drinks consumed to [NA] # change [99] for drinkfreq to [NA] # change [9] for smokefreq to [NA] mddold$sex[mddold$sex==2]=0 # sex=0 female; sex=1 male mddold$sedatives[mddold$sedatives==2]=0 mddold$tranquilizers[mddold$tranquilizers==2]=0 mddold$cannabis[mddold$cannabis==2]=0 mddold$opioids[mddold$opioids==2]=0 mddold$amphetamines[mddold$amphetamines==2]=0 mddold$cocaine[mddold$cocaine==2]=0 mddold$heroine[mddold$heroine==2]=0 mddold$hallucinogens[mddold$hallucinogens==2]=0 mddold$inhalants[mddold$inhalants==2]=0 mddold$sedatives[mddold$sedatives==9]=NA mddold$tranquilizers[mddold$tranquilizers==9]=NA mddold$cannabis[mddold$cannabis==9]=NA mddold$opioids[mddold$opioids==9]=NA mddold$amphetamines[mddold$amphetamines==9]=NA mddold$cocaine[mddold$cocaine==9]=NA mddold$heroine[mddold$heroine==9]=NA mddold$hallucinogens[mddold$hallucinogens==9]=NA mddold$inhalants[mddold$inhalants==9]=NA mddold$alcohol[mddold$alcohol==99]=NA mddold$smoke[mddold$smoke==99]=NA mddold$smokefreq[mddold$smokefreq==9]=NA mddold$drinkfreq[mddold$drinkfreq==99]=NA # # # ADD NEW VARIABLES: PACKYEARS and DRINKYEARS # # # mddold$smokefreqyr[mddold$smokefreq==1]= 364 mddold$smokefreqyr[mddold$smokefreq==2]= 286 mddold$smokefreqyr[mddold$smokefreq==3]= 182 mddold$smokefreqyr[mddold$smokefreq==4]= 78 mddold$smokefreqyr[mddold$smokefreq==5]= 30 mddold$smokefreqyr[mddold$smokefreq==6]= 1 mddold$smokefreqyr[mddold$smokefreq==NA]= NA mddold$alcoholfreqyr[mddold$drinkfreq==1]=364 mddold$alcoholfreqyr[mddold$drinkfreq==2]=286 mddold$alcoholfreqyr[mddold$drinkfreq==3]=182 mddold$alcoholfreqyr[mddold$drinkfreq==4]=104 mddold$alcoholfreqyr[mddold$drinkfreq==5]=52 mddold$alcoholfreqyr[mddold$drinkfreq==6]=30 mddold$alcoholfreqyr[mddold$drinkfreq==7]=12 mddold$alcoholfreqyr[mddold$drinkfreq==8]=9 mddold$alcoholfreqyr[mddold$drinkfreq==9]=4.5 mddold$alcoholfreqyr[mddold$drinkfreq==10]=1.5 mddold$alcoholfreqyr[mddold$drinkfreq==NA]=NA mddold$cigsperyear <- (mddold$smokefreqyr * mddold$smoke) mddold$swigsperyear <- (mddold$alcoholfreqyr * mddold$alcohol) summary(mddold$smokefreqyr) summary(mddold$alcoholfreqyr) summary(mddold$cigsperyear) summary(mddold$swigsperyear) table(mddold$smokefreqyr) table(mddold$alcoholfreqyr) table(mddold$cigsperyear) table(mddold$swigsperyear) mddnew <- mddold str(mddnew) # # -------------------------------------------------------------------------------------------------------- # FREQUENCY TABLES # ("mdd", "sex", "drinkyears", "packyears", "sedatives", "tranquilizers", "cannabis", "opioids", "amphetamines", "cocaine", "heroine", "hallucinogens", "inhalants") library(gmodels) CrossTable(mddnew$mdd, mddnew$sex) CrossTable(mddnew$mdd, mddnew$sedatives) CrossTable(mddnew$mdd, mddnew$tranquilizers) CrossTable(mddnew$mdd, mddnew$cannabis) CrossTable(mddnew$mdd, mddnew$opioids) CrossTable(mddnew$mdd, mddnew$amphetamines) CrossTable(mddnew$mdd, mddnew$cocaine) CrossTable(mddnew$mdd, mddnew$heroine) CrossTable(mddnew$mdd, mddnew$hallucinogens) CrossTable(mddnew$mdd, mddnew$inhalants) library(Hmisc) mddnew$g4cigs <- cut2(mddnew$cigsperyear, g = 4) CrossTable(mddnew$mdd, mddnew$g4cigs) # intervals # [ 1, 2002) | [2002, 4550) | [4550, 7644) | [7644,35672] mddnew$g4swigs <- cut2(mddnew$swigsperyear, g = 4) CrossTable(mddnew$mdd, mddnew$g4swigs) # intervals # [ 1.5, 10.5) | [ 10.5, 63.0) | [ 63.0, 360.0) | [360.0,35672.0] # # -------------------------------------------------------------------------------------------------------- # # # MULTIVARIATE GRAPHS FOR EXPLORATORY ANALYSIS # # # library(RColorBrewer) mypar <- function(a = 1, b = 1, brewer.n = 4, brewer.name = "RdYlGn", ...) { par(mar = c(2.5, 2.5, 1.6, 1.1), mgp = c(1.5, 0.5, 0)) par(mfrow = c(a, b), ...) palette(brewer.pal(brewer.n, brewer.name)) } cols4v1 <- brewer.pal(5, "YlGnBu") pal4v1 <- colorRampPalette(cols4v1) # <- dark blue cyan white gradient cols4v2 <- brewer.pal(5, "YlOrBr") pal4v2 <- colorRampPalette(cols4v2) # <- brown orange white gradient cols4v3 <- brewer.pal(5, "PuRd") pal4v3 <- colorRampPalette(cols4v3) # <- purple pink white gradient cols8 <- brewer.pal(8, "Set3") pal8 <- colorRampPalette(cols8) cols12 <- brewer.pal(12, "Set3") pal12 <- colorRampPalette(cols12) # create table for mdd vs cigs category mddvg4cigs = table(mddnew$mdd,mddnew$g4cigs) # To get the graph we want, we need to exchange the rows in this table mddvg4cigs = rbind(mddvg4cigs[2,],mddvg4cigs[1,]) # and turn them into percents (dividing by the num. of observations # in each cigs category) mddvg4cigs[1,]=mddvg4cigs[1,]/table(mddnew$g4cigs) mddvg4cigs[2,]=mddvg4cigs[2,]/table(mddnew$g4cigs) str(mddvg4cigs) # create table for mdd vs alcohol category mddvg4swigs = table(mddnew$mdd,mddnew$g4swigs) # To get the graph we want, we need to exchange the rows in this table mddvg4swigs = rbind(mddvg4swigs[2,],mddvg4swigs[1,]) # and turn them into percents (dividing by the num. of observations # in each swigs category) mddvg4swigs[1,]=mddvg4swigs[1,]/table(mddnew$g4swigs) mddvg4swigs[2,]=mddvg4swigs[2,]/table(mddnew$g4swigs) str(mddvg4swigs) # create table for mdd vs sex mddvsex = table(mddnew$mdd,mddnew$sex) # To get the graph we want, we need to exchange the rows in this table mddvsex = rbind(mddvsex[2,],mddvsex[1,]) # and turn them into percents (dividing by the num. of observations # in either con or sans sex) mddvsex[1,]=mddvsex[1,]/table(mddnew$sex) mddvsex[2,]=mddvsex[2,]/table(mddnew$sex) str(mddvsex) # create table for mdd vs sedatives mddvsedatives = table(mddnew$mdd,mddnew$sedatives) # To get the graph we want, we need to exchange the rows in this table mddvsedatives = rbind(mddvsedatives[2,],mddvsedatives[1,]) # and turn them into percents (dividing by the num. of observations # in either con or sans sedatives) mddvsedatives[1,]=mddvsedatives[1,]/table(mddnew$sedatives) mddvsedatives[2,]=mddvsedatives[2,]/table(mddnew$sedatives) str(mddvsedatives) # create table for mdd vs cannabis mddvcannabis = table(mddnew$mdd,mddnew$cannabis) # To get the graph we want, we need to exchange the rows in this table mddvcannabis = rbind(mddvcannabis[2,],mddvcannabis[1,]) # and turn them into percents (dividing by the num. of observations # in either con or sans cannabis) mddvcannabis[1,]=mddvcannabis[1,]/table(mddnew$cannabis) mddvcannabis[2,]=mddvcannabis[2,]/table(mddnew$cannabis) str(mddvcannabis) # create table for mdd vs opioids mddvopioids = table(mddnew$mdd,mddnew$opioids) # To get the graph we want, we need to exchange the rows in this table mddvopioids = rbind(mddvopioids[2,],mddvopioids[1,]) # and turn them into percents (dividing by the num. of observations # in either con or sans opioids) mddvopioids[1,]=mddvopioids[1,]/table(mddnew$opioids) mddvopioids[2,]=mddvopioids[2,]/table(mddnew$opioids) str(mddvopioids) # create table for mdd vs tranquilizers mddvtranquilizers = table(mddnew$mdd,mddnew$tranquilizers) # To get the graph we want, we need to exchange the rows in this table mddvtranquilizers = rbind(mddvtranquilizers[2,],mddvtranquilizers[1,]) # and turn them into percents (dividing by the num. of observations # in either con or sans tranquilizers) mddvtranquilizers[1,]=mddvtranquilizers[1,]/table(mddnew$tranquilizers) mddvtranquilizers[2,]=mddvtranquilizers[2,]/table(mddnew$tranquilizers) str(mddvtranquilizers) # create table for mdd vs amphetamines mddvamphetamines = table(mddnew$mdd,mddnew$amphetamines) # To get the graph we want, we need to exchange the rows in this table mddvamphetamines = rbind(mddvamphetamines[2,],mddvamphetamines[1,]) # and turn them into percents (dividing by the num. of observations # in either con or sans amphetamines) mddvamphetamines[1,]=mddvamphetamines[1,]/table(mddnew$amphetamines) mddvamphetamines[2,]=mddvamphetamines[2,]/table(mddnew$amphetamines) str(mddvamphetamines) # create table for mdd vs cocaine mddvcocaine = table(mddnew$mdd,mddnew$cocaine) # To get the graph we want, we need to exchange the rows in this table mddvcocaine = rbind(mddvcocaine[2,],mddvcocaine[1,]) # and turn them into percents (dividing by the num. of observations # in either con or sans cocaine) mddvcocaine[1,]=mddvcocaine[1,]/table(mddnew$cocaine) mddvcocaine[2,]=mddvcocaine[2,]/table(mddnew$cocaine) str(mddvcocaine) # create table for mdd vs heroine mddvheroine = table(mddnew$mdd,mddnew$heroine) # To get the graph we want, we need to exchange the rows in this table mddvheroine = rbind(mddvheroine[2,],mddvheroine[1,]) # and turn them into percents (dividing by the num. of observations # in either con or sans heroine) mddvheroine[1,]=mddvheroine[1,]/table(mddnew$heroine) mddvheroine[2,]=mddvheroine[2,]/table(mddnew$heroine) str(mddvheroine) # create table for mdd vs hallucinogens mddvhallucinogens = table(mddnew$mdd,mddnew$hallucinogens) # To get the graph we want, we need to exchange the rows in this table mddvhallucinogens = rbind(mddvhallucinogens[2,],mddvhallucinogens[1,]) # and turn them into percents (dividing by the num. of observations # in either con or sans hallucinogens) mddvhallucinogens[1,]=mddvhallucinogens[1,]/table(mddnew$hallucinogens) mddvhallucinogens[2,]=mddvhallucinogens[2,]/table(mddnew$hallucinogens) str(mddvhallucinogens) # create table for mdd vs inhalants mddvinhalants = table(mddnew$mdd,mddnew$inhalants) # To get the graph we want, we need to exchange the rows in this table mddvinhalants = rbind(mddvinhalants[2,],mddvinhalants[1,]) # and turn them into percents (dividing by the num. of observations # in either con or sans inhalants) mddvinhalants[1,]=mddvinhalants[1,]/table(mddnew$inhalants) mddvinhalants[2,]=mddvinhalants[2,]/table(mddnew$inhalants) str(mddvinhalants) mypar(mfrow = c(2,2)) # MDD diagnosis {RESPONSE} by Estimated Cigarette Use per Year {EXPLANATORY} among all Adults in the NESARC Study bp_mddvg4cigs <- barplot(mddvg4cigs[1,], col=unique(mddnew$g4cigs), xlab="cigarettes per year", ylab="diagnosed depression", cex.axis=0.8) # MDD diagnosis {RESPONSE} by Estimated Cigarette Use per Year {EXPLANATORY} among all Adults in the NESARC Study bp_mddvg4cigs <- barplot(mddvg4cigs, col=unique(mddnew$g4cigs), xlab="cigarettes per year", ylab="diagnosed depression", cex.axis=0.8) # MDD diagnosis {RESPONSE} by Estimated Alcohol Consumed per Year {EXPLANATORY} among all Adults in the NESARC Study bp_mddvg4swigs <- barplot(mddvg4swigs[1,], col=unique(mddnew$g4swigs), xlab="alcohol per year", ylab="diagnosed depression", cex.axis=0.8) # MDD diagnosis {RESPONSE} by Estimated Alcohol Consumed per Year {EXPLANATORY} among all Adults in the NESARC Study bp_mddvg4swigs <- barplot(mddvg4swigs, col=unique(mddnew$g4swigs), xlab="alcohol per year", ylab="diagnosed depression", cex.axis=0.8) #dev.copy2pdf(file="mdd_cigarettes_alcohol.pdf", height =8, width = 11) mypar(mfrow = c(2, 5)) # MDD diagnosis {RESPONSE} by Biological Sex {EXPLANATORY} among all Adults in the NESARC Study bp_mddvsex <- barplot(mddvsex, col=pal4v1(4), xlab="biological sex, 0 - female, 1 - male", ylab="diagnosed depression") # MDD diagnosis {RESPONSE} by Sedatives Use {EXPLANATORY} among all Adults in the NESARC Study bp_mddvsedatives <- barplot(mddvsedatives, col=pal4v2(4), xlab="sedative use", ylab="diagnosed depression") # MDD diagnosis {RESPONSE} by Cannabis Use {EXPLANATORY} among all Adults in the NESARC Study bp_mddvcannabis <- barplot(mddvcannabis, col=pal4v3(4), xlab="cannabis use", ylab="diagnosed depression") # MDD diagnosis {RESPONSE} by tranquilizers use {EXPLANATORY} among all Adults in the NESARC Study bp_mddvtranquilizers <- barplot(mddvtranquilizers, col=pal8(4), xlab="tranquilizers use", ylab="diagnosed depression") # MDD diagnosis {RESPONSE} by opioids use {EXPLANATORY} among all Adults in the NESARC Study bp_mddvopioids <- barplot(mddvopioids, col=pal12(4), xlab="opioids use", ylab="diagnosed depression") # MDD diagnosis {RESPONSE} by cocaine use {EXPLANATORY} among all Adults in the NESARC Study bp_mddvcocaine <- barplot(mddvcocaine, col=pal12(4), xlab="cocaine use", ylab="diagnosed depression") # MDD diagnosis {RESPONSE} by amphetamines use {EXPLANATORY} among all Adults in the NESARC Study bp_mddvamphetamines <- barplot(mddvamphetamines, col=pal8(4), xlab="amphetamines use", ylab="diagnosed depression") # MDD diagnosis {RESPONSE} by heroine use {EXPLANATORY} among all Adults in the NESARC Study bp_mddvheroine <- barplot(mddvheroine, col=pal4v3(4), xlab="heroine use", ylab="diagnosed depression") # MDD diagnosis {RESPONSE} by hallucinogens use {EXPLANATORY} among all Adults in the NESARC Study bp_mddvhallucinogens <- barplot(mddvhallucinogens, col=pal4v2(4), xlab="hallucinogens use", ylab="diagnosed depression") # MDD diagnosis {RESPONSE} by inhalants use {EXPLANATORY} among all Adults in the NESARC Study bp_mddvinhalants <- barplot(mddvinhalants, col=pal4v1(4), xlab="inhalants use", ylab="diagnosed depression") #dev.copy2pdf(file="mdd_sex_substances.pdf", height =8, width = 11) mypar(mfrow=c(1,2)) boxplot(mddnew$cigsperyear, col="#FE9929", xlab = "# of cigarettes smoked per year", cex=1.5) boxplot(mddnew$swigsperyear, log="y", col="#41B6C4", xlab = "# of alcohol consumed per year", cex=1.5) #dev.copy2pdf(file="boxplots cigs and swigs.pdf", height =4, width = 7) # # -------------------------------------------------------------------------------------------------------- # # MODELING # # # SEPARATE MODELS FOR FEMALES AND MALES males <- mddnew[[2]] == 1 females <- mddnew[[2]] == 0 mddnewmales <- mddnew[males,] mddnewfemales <- mddnew[females,] str(mddnewmales) str(mddnewfemales) logsubstancesall <- glm(mdd ~ as.factor(g4swigs) + as.factor(g4cigs) + sedatives + tranquilizers + cannabis + opioids + amphetamines + cocaine + heroine + hallucinogens + inhalants, data=mddnew, family="binomial") logsubstancesmales <- glm(mdd ~ as.factor(g4swigs) + as.factor(g4cigs) + sedatives + tranquilizers + cannabis + opioids + amphetamines + cocaine + heroine + hallucinogens + inhalants, data=mddnewmales, family="binomial") logsubstancesfemales <- glm(mdd ~ as.factor(g4swigs) + as.factor(g4cigs) + sedatives + tranquilizers + cannabis + opioids + amphetamines + cocaine + heroine + hallucinogens + inhalants, data=mddnewfemales, family="binomial") summary(logsubstancesall) exp(logsubstancesall$coeff) exp(confint(logsubstancesall)) plot (mddnew$mdd, logsubstancesall$fitted) anova(logsubstancesall,test="Chisq") summary(logsubstancesmales) exp(logsubstancesmales$coeff) exp(confint(logsubstancesmales)) plot (mddnewmales$mdd, logsubstancesmales$fitted) anova(logsubstancesmales,test="Chisq") # Call: # glm(formula = mdd ~ as.factor(g4swigs) + as.factor(g4cigs) + # sedatives + tranquilizers + cannabis + opioids + amphetamines + # cocaine + heroine + hallucinogens + inhalants, family = "binomial", # data = mddnewmales) # # Deviance Residuals: # Min 1Q Median 3Q Max # -1.3316 -0.5962 -0.4860 -0.4472 2.1951 # # Coefficients: # Estimate Std. Error z value Pr(>|z|) # (Intercept) -2.1097030 0.1136347 -18.566 < 2e-16 *** # as.factor(g4swigs)[ 10.5, 63.0) 0.1165468 0.1174018 0.993 0.320848 # as.factor(g4swigs)[ 63.0, 360.0) -0.1015568 0.1145740 -0.886 0.375409 # as.factor(g4swigs)[360.0,35672.0] -0.1425212 0.1073813 -1.327 0.184428 # as.factor(g4cigs)[2002, 4550) -0.0627617 0.1074292 -0.584 0.559076 # as.factor(g4cigs)[4550, 7644) 0.0330148 0.0902443 0.366 0.714486 # as.factor(g4cigs)[7644,35672] 0.3729660 0.1058383 3.524 0.000425 *** <-- this is slightly significant # sedatives 0.5647761 0.1393265 4.054 5.04e-05 *** <-- this is slightly significant # tranquilizers 0.0351090 0.1557427 0.225 0.821645 # cannabis 0.5819275 0.0845202 6.885 5.78e-12 *** <-- this is slightly significant # opioids 0.3639522 0.1264450 2.878 0.003998 ** <-- this is slightly significant # amphetamines 0.1981210 0.1285637 1.541 0.123308 # cocaine 0.0643056 0.1195646 0.538 0.590693 # heroine -0.0002183 0.2815689 -0.001 0.999382 # hallucinogens 0.1674107 0.1238734 1.351 0.176546 # inhalants 0.1386452 0.1516635 0.914 0.360631 # --- # Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 # (Dispersion parameter for binomial family taken to be 1) # # Null deviance: 5767.9 on 6544 degrees of freedom # Residual deviance: 5442.7 on 6529 degrees of freedom # (11973 observations deleted due to missingness) # AIC: 5474.7 # Number of Fisher Scoring iterations: 4 # > exp(logsubstancesmales$coeff) # (Intercept) as.factor(g4swigs)[ 10.5, 63.0) # 0.1212740 1.1236101 # as.factor(g4swigs)[ 63.0, 360.0) as.factor(g4swigs)[360.0,35672.0] # 0.9034299 0.8671692 # as.factor(g4cigs)[2002, 4550) as.factor(g4cigs)[4550, 7644) # 0.9391673 1.0335659 # as.factor(g4cigs)[7644,35672] sedatives # 1.4520350 1.7590538 # tranquilizers cannabis # 1.0357326 1.7894844 # opioids amphetamines # 1.4390054 1.2191099 # cocaine heroine # 1.0664182 0.9997818 # hallucinogens inhalants # 1.1822398 1.1487165 # > exp(confint(logsubstancesmales)) # Waiting for profiling to be done... # 2.5 % 97.5 % # (Intercept) 0.09672887 0.1510365 # as.factor(g4swigs)[ 10.5, 63.0) 0.89350182 1.4160715 # as.factor(g4swigs)[ 63.0, 360.0) 0.72254676 1.1325033 # as.factor(g4swigs)[360.0,35672.0] 0.70381227 1.0724474 # as.factor(g4cigs)[2002, 4550) 0.76016557 1.1585051 # as.factor(g4cigs)[4550, 7644) 0.86652156 1.2344479 # as.factor(g4cigs)[7644,35672] 1.17957468 1.7864351 <-- this is slightly significant # sedatives 1.33680824 2.3088374 <-- this is slightly significant # tranquilizers 0.76170329 1.4030169 # cannabis 1.51531707 2.1107442 <-- this is slightly significant # opioids 1.12075291 1.8402787 <-- this is slightly significant # amphetamines 0.94609989 1.5664145 # cocaine 0.84226946 1.3461298 # heroine 0.57008024 1.7265223 # hallucinogens 0.92610582 1.5053609 # inhalants 0.85095214 1.5426781 # > plot (mddnewmales$mdd, logsubstancesmales$fitted) # Error in xy.coords(x, y, xlabel, ylabel, log) : # 'x' and 'y' lengths differ # > anova(logsubstancesmales,test="Chisq") # Analysis of Deviance Table # Model: binomial, link: logit # Response: mdd # Terms added sequentially (first to last) # Df Deviance Resid. Df Resid. Dev Pr(>Chi) # NULL 6544 5767.9 # as.factor(g4swigs) 3 4.398 6541 5763.5 0.2215738 # as.factor(g4cigs) 3 19.022 6538 5744.4 0.0002705 *** <-- this is significant # sedatives 1 175.136 6537 5569.3 < 2.2e-16 *** <-- this is significant # tranquilizers 1 14.750 6536 5554.6 0.0001227 *** <-- this is significant # cannabis 1 89.467 6535 5465.1 < 2.2e-16 *** <-- this is significant # opioids 1 12.316 6534 5452.8 0.0004492 *** <-- this is significant # amphetamines 1 5.754 6533 5447.0 0.0164489 * <-- this is significant # cocaine 1 1.310 6532 5445.7 0.2523128 # heroine 1 0.025 6531 5445.7 0.8751546 # hallucinogens 1 2.193 6530 5443.5 0.1386587 # inhalants 1 0.829 6529 5442.7 0.3626344 # --- # Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 summary(logsubstancesfemales) exp(logsubstancesfemales$coeff) exp(confint(logsubstancesfemales)) plot (mddnewfemales$mdd, logsubstancesfemales$fitted) anova(logsubstancesfemales,test="Chisq") # > summary(logsubstancesfemales) # # Call: # glm(formula = mdd ~ as.factor(g4swigs) + as.factor(g4cigs) + # sedatives + tranquilizers + cannabis + opioids + amphetamines + # cocaine + heroine + hallucinogens + inhalants, family = "binomial", # data = mddnewfemales) # # Deviance Residuals: # Min 1Q Median 3Q Max # -1.7306 -0.8047 -0.7157 1.2613 1.7912 # # Coefficients: # Estimate Std. Error z value Pr(>|z|) # (Intercept) -1.23135 0.07360 -16.731 < 2e-16 *** # as.factor(g4swigs)[ 10.5, 63.0) -0.10289 0.07709 -1.335 0.181991 # as.factor(g4swigs)[ 63.0, 360.0) -0.07070 0.07920 -0.893 0.372032 # as.factor(g4swigs)[360.0,35672.0] -0.11143 0.08503 -1.310 0.190061 # as.factor(g4cigs)[2002, 4550) 0.08761 0.07727 1.134 0.256842 # as.factor(g4cigs)[4550, 7644) 0.26987 0.07261 3.716 0.000202 *** # as.factor(g4cigs)[7644,35672] 0.51903 0.10521 4.934 8.08e-07 *** # sedatives 0.23058 0.13434 1.716 0.086080 . # tranquilizers 0.25377 0.14481 1.752 0.079695 . # cannabis 0.60725 0.06880 8.827 < 2e-16 *** # opioids 0.44915 0.12660 3.548 0.000388 *** # amphetamines 0.34957 0.12553 2.785 0.005357 ** # cocaine 0.10326 0.11098 0.931 0.352105 # heroine 0.30044 0.46536 0.646 0.518537 # hallucinogens -0.03703 0.11994 -0.309 0.757524 # inhalants -0.22342 0.19757 -1.131 0.258134 # --- # Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 # (Dispersion parameter for binomial family taken to be 1) # # Null deviance: 7461.1 on 6040 degrees of freedom # Residual deviance: 7153.5 on 6025 degrees of freedom # (18534 observations deleted due to missingness) # AIC: 7185.5 # # Number of Fisher Scoring iterations: 4 # > exp(logsubstancesfemales$coeff) # (Intercept) as.factor(g4swigs)[ 10.5, 63.0) as.factor(g4swigs)[ 63.0, 360.0) # 0.2918978 0.9022268 0.9317417 # as.factor(g4swigs)[360.0,35672.0] as.factor(g4cigs)[2002, 4550) as.factor(g4cigs)[4550, 7644) # 0.8945575 1.0915670 1.3097916 # as.factor(g4cigs)[7644,35672] sedatives tranquilizers # 1.6803973 1.2593351 1.2888798 # cannabis opioids amphetamines # 1.8353745 1.5669720 1.4184636 # cocaine heroine hallucinogens # 1.1087842 1.3504529 0.9636485 # inhalants # 0.7997824 # > exp(confint(logsubstancesfemales)) # Waiting for profiling to be done... # 2.5 % 97.5 % # (Intercept) 0.2524219 0.3368565 # as.factor(g4swigs)[ 10.5, 63.0) 0.7755918 1.0492891 # as.factor(g4swigs)[ 63.0, 360.0) 0.7976125 1.0880415 # as.factor(g4swigs)[360.0,35672.0] 0.7568787 1.0563681 # as.factor(g4cigs)[2002, 4550) 0.9381185 1.2700609 # as.factor(g4cigs)[4550, 7644) 1.1362570 1.5104773 # as.factor(g4cigs)[7644,35672] 1.3663559 2.0641066 # sedatives 0.9667310 1.6374606 # tranquilizers 0.9694174 1.7109332 # cannabis 1.6035308 2.0999779 # opioids 1.2219634 2.0078102 # amphetamines 1.1087079 1.8140419 # cocaine 0.8913747 1.3774276 # heroine 0.5462273 3.4608405 # hallucinogens 0.7609479 1.2179831 # inhalants 0.5414240 1.1761289 # > anova(logsubstancesfemales,test="Chisq") # Analysis of Deviance Table # Model: binomial, link: logit # Response: mdd # Terms added sequentially (first to last) # Df Deviance Resid. Df Resid. Dev Pr(>Chi) # NULL 6040 7461.1 # as.factor(g4swigs) 3 1.950 6037 7459.2 0.582951 # as.factor(g4cigs) 3 34.849 6034 7424.3 1.311e-07 *** # sedatives 1 86.558 6033 7337.8 < 2.2e-16 *** # tranquilizers 1 30.171 6032 7307.6 3.955e-08 *** # cannabis 1 127.106 6031 7180.5 < 2.2e-16 *** # opioids 1 15.441 6030 7165.1 8.511e-05 *** # amphetamines 1 8.984 6029 7156.1 0.002723 ** # cocaine 1 0.711 6028 7155.4 0.399268 # heroine 1 0.288 6027 7155.1 0.591425 # hallucinogens 1 0.252 6026 7154.8 0.615423 # inhalants 1 1.286 6025 7153.5 0.256804 # --- # Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 |
Subscribe to:
Posts (Atom)