wvincent — Sep 30, 2013, 7:44 PM
##Wilson Vincent
##PH 251D
##Chapter 1 Homework
##1.1
##What is the R Workspace file on your operating system?
##It is a file folder in which your work during a session is saved and stored for later use.
##What is the file path to your R workspace file?
##getwd()
##[1] "/Users/wilsonvincent/Dropbox/R Files/Ch1"
##What is the name of this workspace file?
##It is a file folder called Ch1.
##1.2
##By default, which R packages come already loaded? What are the file paths to the default R packages?
##> search()
##[1] ".GlobalEnv" "tools:RGUI" "package:stats" "package:graphics" "package:grDevices" "package:utils" "package:datasets" "package:methods"
##[9] "Autoloads" "package:base"
##What are the file paths to the default R packages?
##> searchpaths()
##[1] ".GlobalEnv" "tools:RGUI"
##[3] "/Library/Frameworks/R.framework/Versions/2.15/Resources/library/stats" "/Library/Frameworks/R.framework/Versions/2.15/Resources/library/graphics"
##[5] "/Library/Frameworks/R.framework/Versions/2.15/Resources/library/grDevices" "/Library/Frameworks/R.framework/Versions/2.15/Resources/library/utils"
##[7] "/Library/Frameworks/R.framework/Versions/2.15/Resources/library/datasets" "/Library/Frameworks/R.framework/Versions/2.15/Resources/library/methods"
##[9] "Autoloads"
##1.3
##List all the objects in the current workspace.
##> ls()
##[1] "centimeters" "dates" "inches" "z"
##Using one expression, remove all the objects in the current workspace.
##> rm(list = ls())
##To check the workspace to determine if the objects were deleted, I used the following command to get the following response:
##> ls()
##character(0)
##1.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 <- 1:12
##> centimeters <- inches/2.54
##> cbind(inches, centimeters)
##inches centimeters
##[1,] 1 0.3937008
##[2,] 2 0.7874016
##[3,] 3 1.1811024
##[4,] 4 1.5748031
##[5,] 5 1.9685039
##[6,] 6 2.3622047
##[7,] 7 2.7559055
##[8,] 8 3.1496063
##[9,] 9 3.5433071
##[10,] 10 3.9370079
##[11,] 11 4.3307087
##[12,] 12 4.7244094
##1.5
##What are the freezing and boiling points of water in degrees Fahrenheit?
##> celsius <- c(0,100)
##1.6
##> celsius
##[1] 0 100
##> fahrenheit <- (9/5) * celsius + 32
##> fahrenheit
##[1] 32 212
##1.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.
##> celsius <- seq(0, 100, 5)
##> celsius
##[1] 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100
##> fahrenheit <- (9/5) * celsius + 32
##> fahrenheit
##[1] 32 41 50 59 68 77 86 95 104 113 122 131 140 149 158 167 176 185 194 203 212
##> cbind(celsius, fahrenheit)
##celsius fahrenheit
##[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.7
##Calculate your BMI (donC"b,b"t report it to us).
##> my_wght_lbs <- ***
##> my_wght_kg <- my_wght_lbs/2.2
##> my_wght_kg
##[1] --------
##> my_hght_in <- 68
##> my_hght_ft <- 68/12
##> my_hght_m <- my_hght_ft/3.3
##> my_hght_m
##[1] 1.717172
##> my_bmi <- my_wght_kg/(my_hght_m)^2
##> my_bmi
##[1] --------
##1.8
##Using Table 1.1 on page 8, explain in words, and use R to illustrate, the difference between modulus and integer divide.
##>
##> 5/2
##[1] 2.5
##> 5%/%2
##[1] 2
##> 5%%2
##[1] 1
##> 10/2
##[1] 5
##>
##> 10%/%2
##[1] 5
##>
##> 10%%2
##[1] 0
##> 16/3
##[1] 5.333333
##> 16%/%3
##[1] 5
##> 16%%3
##[1] 1
##The interger divide performs division and returns the answer as an interger.
##The modulus divide provides the remainder of the division.
##1.9
##What kind of generalizations can you make about the natural logarithm and its baseb
##The number e, or 2.71828, has at least several notable properties, based on my review of the graph. For example, loge(1) = 0, and loge(e) = 1. Whereas x values in the graph appear to range from 0 to positive infinity (the theoretical, vertical line at x = 0 serving as an asymptote), the values of loge(x)range seem to range from negative infinity to positive infinity. Also, the curve suggests an expontial relationship between x and loge(x), which makes sense given the exponential nature of the relationship between x and loge(x) (e.g., loge(x) = 2 can be converted to x = e2).
##1.10
##The log transformation of the odds makes it so that the dashed horizontal line at R-(1-R) = 0 is not longer asymptotic, with the curve approaching 0 as the value of R moves toward negative infinity. With the log transformation, the theortical, vertical line at R= 0 becomes asymptotic, and R ranges from 0 to positive infinity, while log(R/[1-R]) ranges from negative infinity to positive infinity. Also, we at R = 0.5, log(R/[1-R]) = 0, and R ranges from 0 to 1, allowing us to work with it as a probability ranging from 0 to 1.
##1.11
##Do these cumulative risks make intuitive sense? Why or why not?
##> n <- 365
##> per.act.risk <- c(0.5, 1, 5, 6.5, 10, 30, 50, 67)/10000
##> risks <- 1 - (1-per.act.risk)^n
##> risks
##[1] 0.01808493 0.03584367 0.16685338 0.21126678 0.30593011
##[6] 0.66601052 0.83951869 0.91402762
##> act <- c("IOI","ROI","IPVI","IAI","RPVI","PNS","RAI","IDU")
##> names(risks) <-act
##> risks
##IOI ROI IPVI IAI RPVI
##0.01808493 0.03584367 0.16685338 0.21126678 0.30593011
##PNS RAI IDU
##0.66601052 0.83951869 0.91402762
##Yes, these make intuitive sense in that the cumulative risk is higher for riskier activities (e.g., receptive anal intercourse [more risky] versus insertive oral intercourse [less risky]).
##1.12
##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.
##> source("C:/Users/wvincent/Desktop/Ch1Job.R")
##> source("C:/Users/wvincent/Desktop/Ch1Job.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
##> risks
##[1] 0.01808493 0.03584367 0.16685338 0.21126678 0.30593011
##[6] 0.66601052 0.83951869 0.91402762
##> act <- c("IOI","ROI","IPVI","IAI","RPVI","PNS","RAI","IDU")
##> names(risks) <-act
##> risks
##IOI ROI IPVI IAI RPVI
##0.01808493 0.03584367 0.16685338 0.21126678 0.30593011
##PNS RAI IDU
##0.66601052 0.83951869 0.91402762
##When I ran the source command, it did run R commands in a source file. However, it did not "echo" the input and out put (i.e., did not show me the input and output) until I specified echo = TRUE.
##1.13
##Examine the log files and describe what happened.
##The sink command sends to a log file what would otherwise be displayed on screen in the console of R. When I did not include echo = TRUE, the log file was blank, as it was in the console. When I did include echo = TRUE, the log file contained the input and output that normally would have appeared in the console without the sink command.
##1.14
##Create a new job file (job02.R) with the following code. Source this file at the R command line and describes what happens.
##Nothing appeared on the screen other than the source code that I used. Typing in show command did show me what was in the object risks on the screen in the R console.