1:8:1 What is the R workspace file on your operating systems? What is the file path to your R workspace file? What is the name of this workspace file?
My computer is a Mac, so the R workspace file is RStudio-0.99.903
getwd()
## [1] "/Users/Michelle/Desktop"
1:8:2 By default, which R packages come already loaded? What are the file paths to the default R packages?
search()
## [1] ".GlobalEnv" "package:stats" "package:graphics"
## [4] "package:grDevices" "package:utils" "package:datasets"
## [7] "package:methods" "Autoloads" "package:base"
1:8:3 List all the object in the current workspace. If there are none, create some data objects. Using one expression, remove all the objects in the current workspace.
Name<-c("Eileen", "Michelle", "Tom", "Nicki")
Role<-c("Mother", "Daughter", "Son", "Daugter")
Occupation<-c("Engineer", "Clerk", "Ambulance Driver", "Medical Assistant")
AgeYears<-c(60, 30, 23, 21)
AgeMonths<-c(AgeYears*12)
Family<-data.frame(Name, Role,Occupation,AgeYears,AgeMonths)
Family
## Name Role Occupation AgeYears AgeMonths
## 1 Eileen Mother Engineer 60 720
## 2 Michelle Daughter Clerk 30 360
## 3 Tom Son Ambulance Driver 23 276
## 4 Nicki Daugter Medical Assistant 21 252
cbind(Name, Role,Occupation,AgeYears,AgeMonths)
## Name Role Occupation AgeYears AgeMonths
## [1,] "Eileen" "Mother" "Engineer" "60" "720"
## [2,] "Michelle" "Daughter" "Clerk" "30" "360"
## [3,] "Tom" "Son" "Ambulance Driver" "23" "276"
## [4,] "Nicki" "Daugter" "Medical Assistant" "21" "252"
ls()
## [1] "AgeMonths" "AgeYears" "Family" "Name" "Occupation"
## [6] "Role"
rm(list = ls()); ls()
## character(0)
1:8:4 One inch equals 2.54 centimeters. Correct the following R code and create a conversion table.
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
1:8:5 At standard temperature and pressure, the freezing and boiling points of water are 0 and 100 degrees Celsius, respectively. What are the freezing and boiling points of water in degrees Fahrenheit?
Celsius<-c(0,100)
Farenheit<-c((9/5*Celsius)+32)
Farenheit
## [1] 32 212
1:8:6 For the Celsius temperatures 0, 5, 10, 15, 20, 25, …, 80, 85, 90, 95, 100, construct a conversion table that displays the corresponding Fahrenheit temperatures. Hint: to create the sequence of Celsius temperatures use seq(0, 100, 5).
Celsius<-c(seq(0,100,5))
Farenheit<-c((9/5*Celsius)+32)
Temp_Table<-cbind(Celsius,Farenheit)
Temp_Table
## Celsius Farenheit
## [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
1:8:7 Calculate your BMI (don’t report it to us). Using Table 1.2, explain in words, and use R to illustrate, the difference between modulus and integer divide.
WeightLbs<-c(seq(120,220, 10))
HeightFt<-c(5.83)
WeightKg<-c(WeightLbs/2.2)
HeightM<-c(HeightFt/3.3)
BMI<-c(WeightKg/(HeightM^2))
BMI_Table<-cbind(WeightLbs, HeightFt, BMI)
BMI_Table
## WeightLbs HeightFt BMI
## [1,] 120 5.83 17.47629
## [2,] 130 5.83 18.93265
## [3,] 140 5.83 20.38901
## [4,] 150 5.83 21.84537
## [5,] 160 5.83 23.30172
## [6,] 170 5.83 24.75808
## [7,] 180 5.83 26.21444
## [8,] 190 5.83 27.67080
## [9,] 200 5.83 29.12716
## [10,] 210 5.83 30.58351
## [11,] 220 5.83 32.03987
-10.5/3 #divide
## [1] -3.5
“Divide” divides and gives you a decimal if necessary
-10.5%%3 #modulus
## [1] 1.5
“Modulus” is another term for absolute value and does something
-10.5%/%3 #integer divide
## [1] -4
“Integer Divide” divides and gives you the closest integer
1:8:8 In R, the log function is to the base e. Implement the following R code and study the graph. What kind of generalizations can you make about the natural logarithm and its base—the number e?
curve(log(x), 0, 6)
abline(v = c(1, exp(1)), h = c(0, 1), lty = 2)
I can certainly make the observation that high school math was a long time ago.
1:8:9 Use the following code to plot the odds.
curve(x/(1-x), 0, 1)
curve(log(x/(1-x)), 0, 1)
What kind of generalizations can you make about the log(odds) as a transformation of risk? Don’t really create a true model of risk.
For each non-blood transfusion transmission probability (per act risk) in Table 1.7, calculate the cumulative risk of being infected after one year (365 days) if one carries out the same act once daily for one year with an HIV-infected partner. Do these cumulative risks make intuitive sense? Why or why not?
Exposure_Route<-c("IDU", "RAI", "PNS", "RPVI", "IAI","IPVI", "ROI", "IOI")
Prob_Infection_1<-c(67/10000, 50/10000, 30/10000, 10/10000, 6.5/10000, 5/10000, 1/10000, 0.5/10000)
Prob_Infection_365<-c(1-(1-Prob_Infection_1)^365)
Prob_Table<- data.frame (Exposure_Route, Prob_Infection_1, Prob_Infection_365)
Prob_Table
## Exposure_Route Prob_Infection_1 Prob_Infection_365
## 1 IDU 0.00670 0.91402762
## 2 RAI 0.00500 0.83951869
## 3 PNS 0.00300 0.66601052
## 4 RPVI 0.00100 0.30593011
## 5 IAI 0.00065 0.21126678
## 6 IPVI 0.00050 0.16685338
## 7 ROI 0.00010 0.03584367
## 8 IOI 0.00005 0.01808493
1:8:10 The source function in R is used to “source” (read in) ASCII text files. Take a group of R commands that worked from a previous problem above and paste them into an ASCII text file and save it with the name job01.R. Then from R command line, source the file. Here is how it looked on my Linux computer running R:
source("/Users/Michelle/Desktop/Berkeley MPH/Fall 2016/R for Epi's/ASCI_1")
Describe what happened. I received a message in the console that that repeated the source code I just ran, and nothing more.
Now, set option to:
source("/Users/Michelle/Desktop/Berkeley MPH/Fall 2016/R for Epi's/ASCI_1", echo = TRUE)
##
## > Name <- c("Eileen", "Michelle", "Tom", "Nicki")
##
## > Role <- c("Mother", "Daughter", "Son", "Daugter")
##
## > Occupation <- c("Engineer", "Clerk", "Ambulance Driver",
## + "Medical Assistant")
##
## > AgeYears <- c(60, 30, 23, 21)
##
## > AgeMonths <- c(AgeYears * 12)
##
## > Family <- data.frame(Name, Role, Occupation, AgeYears,
## + AgeMonths)
##
## > Family
## Name Role Occupation AgeYears AgeMonths
## 1 Eileen Mother Engineer 60 720
## 2 Michelle Daughter Clerk 30 360
## 3 Tom Son Ambulance Driver 23 276
## 4 Nicki Daugter Medical Assistant 21 252
Describe what happened. I received a message in the console that that repeated the source code, as well as the saved code’s text and its output.
1:8:11 Now run the source again (without and with echo = TRUE) but each time create a log file using the sink function. Create two log files: job01.log1a and job01.log1b.
sink("/Users/Michelle/Desktop/Berkeley MPH/Fall 2016/R for Epi's/ASCI_1.log1a")
source("/Users/Michelle/Desktop/Berkeley MPH/Fall 2016/R for Epi's/ASCI_1")
sink() #closes connection
sink("/Users/Michelle/Desktop/Berkeley MPH/Fall 2016/R for Epi's/ASCI_1.log1b")
source("/Users/Michelle/Desktop/Berkeley MPH/Fall 2016/R for Epi's/ASCI_1", echo = TRUE)
##
## > Name <- c("Eileen", "Michelle", "Tom", "Nicki")
##
## > Role <- c("Mother", "Daughter", "Son", "Daugter")
##
## > Occupation <- c("Engineer", "Clerk", "Ambulance Driver",
## + "Medical Assistant")
##
## > AgeYears <- c(60, 30, 23, 21)
##
## > AgeMonths <- c(AgeYears * 12)
##
## > Family <- data.frame(Name, Role, Occupation, AgeYears,
## + AgeMonths)
##
## > Family
## Name Role Occupation AgeYears AgeMonths
## 1 Eileen Mother Engineer 60 720
## 2 Michelle Daughter Clerk 30 360
## 3 Tom Son Ambulance Driver 23 276
## 4 Nicki Daugter Medical Assistant 21 252
sink() #closes connection
Examine the log files and describe what happened. Two new files were created, but I don’t have software to read them so I am not sure of their contents. I will run a source on both files to view them here:
source("/Users/Michelle/Desktop/Berkeley MPH/Fall 2016/R for Epi's/ASCI_1.log1a", echo = TRUE)
source("/Users/Michelle/Desktop/Berkeley MPH/Fall 2016/R for Epi's/ASCI_1.log1b", echo = TRUE)
Maybe my stuff is broken. I get an error.
1:8:12 Create a new job file () with the following code. Source this file at the R command line and describes what happens.
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.01808493 0.03584367 0.16685338 0.21126678 0.30593011 0.66601052
## [7] 0.83951869 0.91402762
source("/Users/Michelle/Desktop/Berkeley MPH/Fall 2016/R for Epi's/ASCI_2", 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.01808493 0.03584367 0.16685338 0.21126678 0.30593011 0.66601052
## [7] 0.83951869 0.91402762
It also runs the code as expected.