Exercise 1
getwd()
## [1] "G:/Graduate School/School Work/Fall 2014/PH251D"
ls()
## character(0)
On my computer the name of the workspace file is just .RData.
Exercise 2
search()
## [1] ".GlobalEnv" "package:stats" "package:graphics"
## [4] "package:grDevices" "package:utils" "package:datasets"
## [7] "package:methods" "Autoloads" "package:base"
searchpaths()
## [1] ".GlobalEnv"
## [2] "C:/Program Files/R/R-3.1.1/library/stats"
## [3] "C:/Program Files/R/R-3.1.1/library/graphics"
## [4] "C:/Program Files/R/R-3.1.1/library/grDevices"
## [5] "C:/Program Files/R/R-3.1.1/library/utils"
## [6] "C:/Program Files/R/R-3.1.1/library/datasets"
## [7] "C:/Program Files/R/R-3.1.1/library/methods"
## [8] "Autoloads"
## [9] "C:/PROGRA~1/R/R-31~1.1/library/base"
Exercise 3
ls()
## character(0)
Above is the code and result to find all the objects in my current workspace, they are from a swirl training session I was doing.
rm(list=ls())
The above command revmoed all objects I created.
Exercise 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
Exercise 5
Celsius<-c(0,100)
Fahrenheit<-(((9/5)*Celsius)+32)
boil.freeze<-cbind(Celsius,Fahrenheit)
rownames(boil.freeze)<-c("Freeze Point","Boiling Point")
boil.freeze
## Celsius Fahrenheit
## Freeze Point 0 32
## Boiling Point 100 212
Exercise 6
Celsius.1<-seq(0,100,5)
Fahrenheit.1<-(((9/5)*Celsius.1)+32)
ctof<-cbind(Celsius.1,Fahrenheit.1)
ctof
## Celsius.1 Fahrenheit.1
## [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
Exercise 7
BMI<-61.235/(1.6891^2)
BMI
## [1] 21.46
Exercise 8 The modulus is the operator between two numbers that gives the remainder (if any) of division of those two numbers.
9%%3
## [1] 0
10%%3
## [1] 1
15%%3
## [1] 0
16%%3
## [1] 1
The integer divide, on the other hand, is simply the number of times that one number will divide into another.
9%/%3
## [1] 3
10%/%3
## [1] 3
15%/%3
## [1] 5
16%/%3
## [1] 5
Exercise 9
curve(log(x), 0, 6)
abline(v = c(1, exp(1)), h = c(0, 1), lty = 2)
From mathematics, we know that e is an infinite series of the form 1+1/1+1/(1+2)+1/(1+2+3)+…, whereas the natural logarithm is of a number is the exponent of e that returns us the original number (eg; ln(5)=1.61, which means that e^1.61=5). From the plot we can see that as X approaches 0, the natural logarithm rapidly goes to negative infinity, whereas as X increases, the natural logarithm increases much more slowly.
Exercise 10
curve(x/(1-x), 0, 1)
curve(log(x/(1-x)), 0, 1)
It seems that using the log(OR) as an estimate of risk smooths out the curve, as well as centering it around zero, which is likely useful for small sample sizes when small changes in numbers of subjects can have dramatic effects on output.
Exercise 11
risk.per.10k.exposure<-c(9000,67,50,30,10,6.5,5,1,0.5)
prob.noinfect<-(1-(risk.per.10k.exposure/10000))
prob.noinfect.1year<-(prob.noinfect^365)
HIV.table<-cbind(risk.per.10k.exposure,prob.noinfect,prob.noinfect.1year)
HIV.table
## risk.per.10k.exposure prob.noinfect prob.noinfect.1year
## [1,] 9000.0 0.1000 0.00000
## [2,] 67.0 0.9933 0.08597
## [3,] 50.0 0.9950 0.16048
## [4,] 30.0 0.9970 0.33399
## [5,] 10.0 0.9990 0.69407
## [6,] 6.5 0.9993 0.78873
## [7,] 5.0 0.9995 0.83315
## [8,] 1.0 0.9999 0.96416
## [9,] 0.5 1.0000 0.98192
This seems to make sense; assuming each event is independent and identically distributed, one could calculate the conditional survival probabities after 1 year in this method (eg; survival on 365th day is conditional on surviving the previous 364 days).
Exercise 12
source("C:/Users/Sean/Desktop/job01.R")
source("C:/Users/Sean/Desktop/job01.R",echo=TRUE)
##
## > risk.per.10k.exposure <- c(9000, 67, 50, 30, 10, 6.5,
## + 5, 1, 0.5)
##
## > prob.noinfect <- (1 - (risk.per.10k.exposure/10000))
##
## > prob.noinfect.1year <- (prob.noinfect^365)
##
## > HIV.table <- cbind(risk.per.10k.exposure, prob.noinfect,
## + prob.noinfect.1year)
##
## > HIV.table
## risk.per.10k.exposure prob.noinfect prob.noinfect.1year
## [1,] 9000.0 0.1000 0.00000
## [2,] 67.0 0.9933 0.08597
## [3,] 50.0 0.9950 0.16048
## [4,] 30.0 0.9970 0.33399
## [5,] 10.0 0.9990 0.69407
## [6,] 6.5 0.9993 0.78873
## [7,] 5.0 0.9995 0.83315
## [8,] 1.0 0.9999 0.96416
## [9,] 0.5 1.0000 0.98192
After the first time running the code nothing appeared to happen (I assume it was read in, but nothing was shown on the R console.) However after adding echo=TRUE, the code ran itself on the console. After reading the help file, I learned that echo when set to true causes R to print the code.
Exercise 13
sink("C:/Users/Sean/Desktop/job01.log1a")
source("C:/Users/Sean/Desktop/job01.R")
sink()
The log file (opened with notepad) appears to be empty.
sink("C:/Users/Sean/Desktop/job01.log1b")
source("C:/Users/Sean/Desktop/job01.R",echo=TRUE)
##
## > risk.per.10k.exposure <- c(9000, 67, 50, 30, 10, 6.5,
## + 5, 1, 0.5)
##
## > prob.noinfect <- (1 - (risk.per.10k.exposure/10000))
##
## > prob.noinfect.1year <- (prob.noinfect^365)
##
## > HIV.table <- cbind(risk.per.10k.exposure, prob.noinfect,
## + prob.noinfect.1year)
##
## > HIV.table
## risk.per.10k.exposure prob.noinfect prob.noinfect.1year
## [1,] 9000.0 0.1000 0.00000
## [2,] 67.0 0.9933 0.08597
## [3,] 50.0 0.9950 0.16048
## [4,] 30.0 0.9970 0.33399
## [5,] 10.0 0.9990 0.69407
## [6,] 6.5 0.9993 0.78873
## [7,] 5.0 0.9995 0.83315
## [8,] 1.0 0.9999 0.96416
## [9,] 0.5 1.0000 0.98192
sink()
The log file is filled with the output of the source file code.
Exercise 14
source("C:/Users/Sean/Desktop/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
Output is risks from the HIV data.