AGonzales251DHW1.R

Allison Gonzales — Sep 21, 2013, 10:32 AM

## Allison Gonzales
## PH 251D
## Homework 1 
## 23 September 2013

##Prob 1.1.
getwd()
[1] "C:/Users/Allie/Documents/MPH Fall 2013/PH 251D/Assignments"

##Prob 1.2.
searchpaths()
 [1] ".GlobalEnv"                                      
 [2] "C:/Users/Allie/Documents/R/win-library/3.0/knitr"
 [3] "C:/Program Files/R/R-3.0.1/library/stats"        
 [4] "C:/Program Files/R/R-3.0.1/library/graphics"     
 [5] "C:/Program Files/R/R-3.0.1/library/grDevices"    
 [6] "C:/Program Files/R/R-3.0.1/library/utils"        
 [7] "C:/Program Files/R/R-3.0.1/library/datasets"     
 [8] "C:/Program Files/R/R-3.0.1/library/methods"      
 [9] "Autoloads"                                       
[10] "C:/PROGRA~1/R/R-30~1.1/library/base"             

##Prob 1.3.
x <- 1
y <- 2*x
ls()
[1] "x" "y"
rm(list = ls())

##Prob 1.4.
inches <- 1:12
centimeters <- inches*2.54
cbind(inches, centimeters)
      inches centimeters
 [1,]      1        2.54
 [2,]      2        5.08
 [3,]      3        7.62
 [4,]      4       10.16
 [5,]      5       12.70
 [6,]      6       15.24
 [7,]      7       17.78
 [8,]      8       20.32
 [9,]      9       22.86
[10,]     10       25.40
[11,]     11       27.94
[12,]     12       30.48

##Prob 1.5.
degC <- 0:100
degF <- (9/5)*degC+32
cbind(degC, degF)
       degC  degF
  [1,]    0  32.0
  [2,]    1  33.8
  [3,]    2  35.6
  [4,]    3  37.4
  [5,]    4  39.2
  [6,]    5  41.0
  [7,]    6  42.8
  [8,]    7  44.6
  [9,]    8  46.4
 [10,]    9  48.2
 [11,]   10  50.0
 [12,]   11  51.8
 [13,]   12  53.6
 [14,]   13  55.4
 [15,]   14  57.2
 [16,]   15  59.0
 [17,]   16  60.8
 [18,]   17  62.6
 [19,]   18  64.4
 [20,]   19  66.2
 [21,]   20  68.0
 [22,]   21  69.8
 [23,]   22  71.6
 [24,]   23  73.4
 [25,]   24  75.2
 [26,]   25  77.0
 [27,]   26  78.8
 [28,]   27  80.6
 [29,]   28  82.4
 [30,]   29  84.2
 [31,]   30  86.0
 [32,]   31  87.8
 [33,]   32  89.6
 [34,]   33  91.4
 [35,]   34  93.2
 [36,]   35  95.0
 [37,]   36  96.8
 [38,]   37  98.6
 [39,]   38 100.4
 [40,]   39 102.2
 [41,]   40 104.0
 [42,]   41 105.8
 [43,]   42 107.6
 [44,]   43 109.4
 [45,]   44 111.2
 [46,]   45 113.0
 [47,]   46 114.8
 [48,]   47 116.6
 [49,]   48 118.4
 [50,]   49 120.2
 [51,]   50 122.0
 [52,]   51 123.8
 [53,]   52 125.6
 [54,]   53 127.4
 [55,]   54 129.2
 [56,]   55 131.0
 [57,]   56 132.8
 [58,]   57 134.6
 [59,]   58 136.4
 [60,]   59 138.2
 [61,]   60 140.0
 [62,]   61 141.8
 [63,]   62 143.6
 [64,]   63 145.4
 [65,]   64 147.2
 [66,]   65 149.0
 [67,]   66 150.8
 [68,]   67 152.6
 [69,]   68 154.4
 [70,]   69 156.2
 [71,]   70 158.0
 [72,]   71 159.8
 [73,]   72 161.6
 [74,]   73 163.4
 [75,]   74 165.2
 [76,]   75 167.0
 [77,]   76 168.8
 [78,]   77 170.6
 [79,]   78 172.4
 [80,]   79 174.2
 [81,]   80 176.0
 [82,]   81 177.8
 [83,]   82 179.6
 [84,]   83 181.4
 [85,]   84 183.2
 [86,]   85 185.0
 [87,]   86 186.8
 [88,]   87 188.6
 [89,]   88 190.4
 [90,]   89 192.2
 [91,]   90 194.0
 [92,]   91 195.8
 [93,]   92 197.6
 [94,]   93 199.4
 [95,]   94 201.2
 [96,]   95 203.0
 [97,]   96 204.8
 [98,]   97 206.6
 [99,]   98 208.4
[100,]   99 210.2
[101,]  100 212.0
##Freezing point is 32degF and boiling point is 212degF.

##Prob 1.6.
degCby5 <- seq(0, 100, 5)
degF <- (9/5)*degCby5 + 32
cbind(degCby5, degF)
      degCby5 degF
 [1,]       0   32
 [2,]       5   41
 [3,]      10   50
 [4,]      15   59
 [5,]      20   68
 [6,]      25   77
 [7,]      30   86
 [8,]      35   95
 [9,]      40  104
[10,]      45  113
[11,]      50  122
[12,]      55  131
[13,]      60  140
[14,]      65  149
[15,]      70  158
[16,]      75  167
[17,]      80  176
[18,]      85  185
[19,]      90  194
[20,]      95  203
[21,]     100  212

##Prob 1.7.
kg <- 45:90
m <- 1:2
BMI <- kg/m^2

##Prob 1.8.

##Modulus divides the first by the second number and the gives the remainder only.
10%%3
[1] 1

##Integer divide divides the first number by the second number and gives the largest
## integer the first number can be divided into as the answer.
10%/%3
[1] 3

##Prob 1.9.
curve(log(x), 0, 6)
abline(v = c(1, exp(1)), h = c(0,1), lty = 2)

plot of chunk unnamed-chunk-1

##Ln and e are inverses of each other.

##Prob 1.10.
curve(x/(1-x), 0, 1)

plot of chunk unnamed-chunk-1

curve(log(x/(1-x)), 0, 1)

plot of chunk unnamed-chunk-1

##Log(odds) allows the risk to be interpreted
##as a negative or positive association on either 
##side of zero.

##Prob 1.11.
##IDU
1-(1-(67/10000))^365
[1] 0.914
##RAI
1-(1-(50/10000))^365
[1] 0.8395
##PNS
1-(1-(30/10000))^365
[1] 0.666
##RPVI
1-(1-(10/10000))^365
[1] 0.3059
##IAI
1-(1-(6.5/10000))^365
[1] 0.2113
##IPVI
1-(1-(5/10000))^365
[1] 0.1669
##ROI
1-(1-(1/10000))^365
[1] 0.03584
##IOI
1-(1-(0.5/10000))^365
[1] 0.01808
##These probabilities do make sense because the most risky acts have a higher probability
## of becoming infected.

##Prob 1.12.
source("C:/Users/Allie/My Documents/MPH Fall 2013/PH 251D/Jobs/job01.R")
##The variables were created in my workspace, but there is no output in the console. 
source("C:/Users/Allie/My Documents/MPH Fall 2013/PH 251D/Jobs/job01.R", echo = TRUE)

> inches <- 1:12

> centimeters <- inches * 2.54

> cbind(inches, centimeters)
      inches centimeters
 [1,]      1        2.54
 [2,]      2        5.08
 [3,]      3        7.62
 [4,]      4       10.16
 [5,]      5       12.70
 [6,]      6       15.24
 [7,]      7       17.78
 [8,]      8       20.32
 [9,]      9       22.86
[10,]     10       25.40
[11,]     11       27.94
[12,]     12       30.48
##The code appeared in the console and created output.

##Prob 1.13.
sink("C:/Users/Allie/My Documents/MPH Fall 2013/PH 251D/Jobs/job01.log1a")
source("C:/Users/Allie/My Documents/MPH Fall 2013/PH 251D/Jobs/job01.R")
sink()
##Log file is empty.
sink("C:/Users/Allie/My Documents/MPH Fall 2013/PH 251D/Jobs/job01.log1b")
source("C:/Users/Allie/My Documents/MPH Fall 2013/PH 251D/Jobs/job01.R", echo = TRUE)

> inches <- 1:12

> centimeters <- inches * 2.54

> cbind(inches, centimeters)
      inches centimeters
 [1,]      1        2.54
 [2,]      2        5.08
 [3,]      3        7.62
 [4,]      4       10.16
 [5,]      5       12.70
 [6,]      6       15.24
 [7,]      7       17.78
 [8,]      8       20.32
 [9,]      9       22.86
[10,]     10       25.40
[11,]     11       27.94
[12,]     12       30.48
sink()
##Log file has the code from text file and output from R.

##Prob 1.14.
source("C:/Users/Allie/My Documents/MPH Fall 2013/PH 251D/Jobs/job02.R", echo = TRUE)

> n <- 365

> per.act.risk <- c(0.5, 1, 5, 6.5, 10, 30, 50, 67)/10000

> risks <- 1 - (1 - per.act.risk)^n

> show(risks)
[1] 0.01808 0.03584 0.16685 0.21127 0.30593 0.66601 0.83952 0.91403
##This code gives the cumulative risks for the exposures from Table 1.6