HW 1 1.1
getwd()
## [1] "/Users/kelleypatten/Documents/Rdata/Classes"
working directory and file path is “/Users/kelleypatten/Documents/Rdata” file name is HW1 241D.Rmd 1.2 The packages automatically loaded can be found by
search()
## [1] ".GlobalEnv" "package:stats" "package:graphics"
## [4] "package:grDevices" "package:utils" "package:datasets"
## [7] "package:methods" "Autoloads" "package:base"
the filepath to the packages can be found using:
searchpaths()
## [1] ".GlobalEnv"
## [2] "/Library/Frameworks/R.framework/Versions/3.1/Resources/library/stats"
## [3] "/Library/Frameworks/R.framework/Versions/3.1/Resources/library/graphics"
## [4] "/Library/Frameworks/R.framework/Versions/3.1/Resources/library/grDevices"
## [5] "/Library/Frameworks/R.framework/Versions/3.1/Resources/library/utils"
## [6] "/Library/Frameworks/R.framework/Versions/3.1/Resources/library/datasets"
## [7] "/Library/Frameworks/R.framework/Versions/3.1/Resources/library/methods"
## [8] "Autoloads"
## [9] "/Library/Frameworks/R.framework/Resources/library/base"
or for a specific package, can use
library(help = grDevices)
(for example)—using this function will open a information tab on package
x<--10
x2<- c("I", "love", "R", "Programming")
y1<- c(1:10)
y2<- sample (y1, 5, replace = TRUE)
1.3 To view all objects in workspace use
ls()
## [1] "a" "BMI"
## [3] "celsius_to_fahrenheit" "centimeters"
## [5] "iden.3" "inches"
## [7] "inches_cent" "invmat6"
## [9] "logodds" "mat6"
## [11] "metadata" "n"
## [13] "Odds" "oddscurve"
## [15] "pie" "seq1"
## [17] "stiff" "tableFahr"
## [19] "x" "x2"
## [21] "y1" "y2"
1.4 Converting inches to centimeters and placing in 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.5 Converting from Celsius to Fahrenheit
celsius_to_fahrenheit<- function(celsius){
return(((9/5)*celsius)+32)}
celsius_to_fahrenheit(100)
## [1] 212
celsius_to_fahrenheit(0)
## [1] 32
1.6 Table of Fahrenheit and Celsius
seq1<-seq(0,100,5)
tableFahr<- cbind(seq1,celsius_to_fahrenheit(seq1))
tableFahr
## seq1
## [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 BMI calculation
BMI<-function(lbs,height){
return((lbs/2.2)/((height/39.37)^2))}
1.8 Difference between integer division(%/%) and modulus function (%%). Integer division will return the value of dividing two integers, however if a remainder is left, this will not be indicated. The modulus function will give the remainder value after dividing 2 integers. For example:
#normal division
11/3
## [1] 3.667
#integer division
11%/%3
## [1] 3
#modulus function
11%%3
## [1] 2
1.9 Exploring the log function (default is natural log)
curve(log(x),1,6)
abline(v=c(1,exp(1)), h=c(0,1), lty=2)
From this graph we can conclude that the value (e) is about 2.7, given that this graph’s x intercept is around 2.7. In the function, this would indicate that ln(~2.7)=1 We can also conclude that the slope of this function is concave down.
1.10 Looking at the calculation for Odds using Risk Ratios. Risk is a probability between 0 and 1. Odds is the relationship then between a risk probability and 1-risk for a given outcome. Odds = (R/(1-R))
Odds <- function(Risk){
return(Risk/(1-Risk))}
curve(Odds,0,1)
curve(log(Odds(x)),0,1)
can infer from this graph that the log transformation of risk is a curve that has two inflection points. The fold changes in log odds are greatly reduced compared to the absolute odds. 1.11 Cumulative risk of HIV transmission over 1 year period, contact with HIV-infected individual.
Exposure_route<- c("Blood transfusion", "Needle sharing IDU", "Receptive Anal intercourse", "Percutaneous needle stick", "Receptive penile-vaginal intercourse", "Insertive anal intercourse", "Insertive penile-vaginal intercourse", "Receptive oral intercourse on penis", "Insertive oral intercourse with penis")
Risk<- c(9000,67,50,30,10,6.5,5,1,.5)
probability_infected<-function(p,n){return(1-(1-p)^n)}
probability_not_infected<-function(p,n){return((1-p)^n)}
probability_infected(Risk/10000,365)
## [1] 1.00000 0.91403 0.83952 0.66601 0.30593 0.21127 0.16685 0.03584 0.01808
probability_not_infected(Risk/10000,365)
## [1] 0.00000 0.08597 0.16048 0.33399 0.69407 0.78873 0.83315 0.96416 0.98192
per_act_risk<-matrix(c(Exposure_route, Risk,c(probability_infected(Risk/10000,365)), c(probability_not_infected(Risk/10000,365))),ncol=4)
colnames(per_act_risk)<-c("Exposure_route","Risk/10,000 exposure","Prob infected/yr","Prob not infected/yr")
per_act_risk
## Exposure_route Risk/10,000 exposure
## [1,] "Blood transfusion" "9000"
## [2,] "Needle sharing IDU" "67"
## [3,] "Receptive Anal intercourse" "50"
## [4,] "Percutaneous needle stick" "30"
## [5,] "Receptive penile-vaginal intercourse" "10"
## [6,] "Insertive anal intercourse" "6.5"
## [7,] "Insertive penile-vaginal intercourse" "5"
## [8,] "Receptive oral intercourse on penis" "1"
## [9,] "Insertive oral intercourse with penis" "0.5"
## Prob infected/yr Prob not infected/yr
## [1,] "1" "0"
## [2,] "0.914027620481329" "0.0859723795186711"
## [3,] "0.839518685795839" "0.160481314204161"
## [4,] "0.66601052297303" "0.33398947702697"
## [5,] "0.305930112959526" "0.694069887040474"
## [6,] "0.211266780124636" "0.788733219875364"
## [7,] "0.166853381929375" "0.833146618070625"
## [8,] "0.0358436658079023" "0.964156334192098"
## [9,] "0.0180849252236011" "0.981915074776399"
The idea of cumulative incidence does make sense, although the idea that one could have 100% chance of contracting the disease seems counterintuitive, since an individual level of risk for all exposures is never 100%. 1.12 Sources a txt/ACSS file
source("/Users/kelleypatten/Documents/job01.txt")
source("/Users/kelleypatten/Documents/job01.txt",echo=TRUE)
##
## > Exposure_route <- c("Blood transfusion", "Needle sharing IDU",
## + "Receptive Anal intercourse", "Percutaneous needle stick",
## + "Receptive p ..." ... [TRUNCATED]
##
## > Risk <- c(9000, 67, 50, 30, 10, 6.5, 5, 1, 0.5)
##
## > probability_infected <- function(p, n) {
## + return(1 - (1 - p)^n)
## + }
##
## > probability_not_infected <- function(p, n) {
## + return((1 - p)^n)
## + }
##
## > probability_infected(Risk/10000, 365)
## [1] 1.00000 0.91403 0.83952 0.66601 0.30593 0.21127 0.16685 0.03584 0.01808
##
## > probability_not_infected(Risk/10000, 365)
## [1] 0.00000 0.08597 0.16048 0.33399 0.69407 0.78873 0.83315 0.96416 0.98192
##
## > per_act_risk <- matrix(c(Exposure_route, Risk, c(probability_infected(Risk/10000,
## + 365)), c(probability_not_infected(Risk/10000, 365))), ncol .... [TRUNCATED]
##
## > colnames(per_act_risk) <- c("Exposure_route", "Risk/10,000 exposure",
## + "Prob infected/yr", "Prob not infected/yr")
##
## > per_act_risk
## Exposure_route Risk/10,000 exposure
## [1,] "Blood transfusion" "9000"
## [2,] "Needle sharing IDU" "67"
## [3,] "Receptive Anal intercourse" "50"
## [4,] "Percutaneous needle stick" "30"
## [5,] "Receptive penile-vaginal intercourse" "10"
## [6,] "Insertive anal intercourse" "6.5"
## [7,] "Insertive penile-vaginal intercourse" "5"
## [8,] "Receptive oral intercourse on penis" "1"
## [9,] "Insertive oral intercourse with penis" "0.5"
## Prob infected/yr Prob not infected/yr
## [1,] "1" "0"
## [2,] "0.914027620481329" "0.0859723795186711"
## [3,] "0.839518685795839" "0.160481314204161"
## [4,] "0.66601052297303" "0.33398947702697"
## [5,] "0.305930112959526" "0.694069887040474"
## [6,] "0.211266780124636" "0.788733219875364"
## [7,] "0.166853381929375" "0.833146618070625"
## [8,] "0.0358436658079023" "0.964156334192098"
## [9,] "0.0180849252236011" "0.981915074776399"
In the first instance, without using echo, the data was read in as R code, however was not printed. Once echo was used, the data was viewable in R, including the data that instructed the file to print.
1.13
sink("/Users/kelleypatten/Documents/job01.log1a.txt")
source("/Users/kelleypatten/Documents/job01.txt")
sink()
sink("/Users/kelleypatten/Documents/job01.log1b.txt")
source("/Users/kelleypatten/Documents/job01.txt",echo=TRUE)
##
## > Exposure_route <- c("Blood transfusion", "Needle sharing IDU",
## + "Receptive Anal intercourse", "Percutaneous needle stick",
## + "Receptive p ..." ... [TRUNCATED]
##
## > Risk <- c(9000, 67, 50, 30, 10, 6.5, 5, 1, 0.5)
##
## > probability_infected <- function(p, n) {
## + return(1 - (1 - p)^n)
## + }
##
## > probability_not_infected <- function(p, n) {
## + return((1 - p)^n)
## + }
##
## > probability_infected(Risk/10000, 365)
## [1] 1.00000 0.91403 0.83952 0.66601 0.30593 0.21127 0.16685 0.03584 0.01808
##
## > probability_not_infected(Risk/10000, 365)
## [1] 0.00000 0.08597 0.16048 0.33399 0.69407 0.78873 0.83315 0.96416 0.98192
##
## > per_act_risk <- matrix(c(Exposure_route, Risk, c(probability_infected(Risk/10000,
## + 365)), c(probability_not_infected(Risk/10000, 365))), ncol .... [TRUNCATED]
##
## > colnames(per_act_risk) <- c("Exposure_route", "Risk/10,000 exposure",
## + "Prob infected/yr", "Prob not infected/yr")
##
## > per_act_risk
## Exposure_route Risk/10,000 exposure
## [1,] "Blood transfusion" "9000"
## [2,] "Needle sharing IDU" "67"
## [3,] "Receptive Anal intercourse" "50"
## [4,] "Percutaneous needle stick" "30"
## [5,] "Receptive penile-vaginal intercourse" "10"
## [6,] "Insertive anal intercourse" "6.5"
## [7,] "Insertive penile-vaginal intercourse" "5"
## [8,] "Receptive oral intercourse on penis" "1"
## [9,] "Insertive oral intercourse with penis" "0.5"
## Prob infected/yr Prob not infected/yr
## [1,] "1" "0"
## [2,] "0.914027620481329" "0.0859723795186711"
## [3,] "0.839518685795839" "0.160481314204161"
## [4,] "0.66601052297303" "0.33398947702697"
## [5,] "0.305930112959526" "0.694069887040474"
## [6,] "0.211266780124636" "0.788733219875364"
## [7,] "0.166853381929375" "0.833146618070625"
## [8,] "0.0358436658079023" "0.964156334192098"
## [9,] "0.0180849252236011" "0.981915074776399"
sink()
In looking at the log files, only file job.01.log1b when opened contained information when opened either in text viewer or in R. However, in both cases, the sink function seems to draw information from the source file specified in the directory. This may be useful in changing the location of files and information. 1.14
source("/Users/kelleypatten/Documents/job02.txt")
## [1] 0.01808 0.03584 0.16685 0.21127 0.30593 0.66601 0.83952 0.91403
in this case, even without using the “echo” option, source runs the code that was detailed in R, because it contains a “print” or return data command at the end of the file.