Zhiguang Huo (Caleb)
Wednesday August 31, 2022
## {
## return(x^3)
## }
## $x
## <environment: R_GlobalEnv>
## function (..., na.rm = FALSE) .Primitive("sum")
## NULL
## NULL
## NULL
X <- matrix(c(189, 104, 10845, 10933), nrow=2,
dimnames=list(Treatment=c("Placebo","Aspirin"),
"Myocardial Infarction"=c("Yes", "No")))
X
## Myocardial Infarction
## Treatment Yes No
## Placebo 189 10845
## Aspirin 104 10933
## [1] 1.832054
## [1] 1.832054
odds.ratio2 <- function(X) ## if the function has only one line, don't even need brackets
X[1,1]*X[2,2]/(X[1,2]*X[2,1])
odds.ratio2(X)
## [1] 1.832054
odds.ratio <- function(X, conf.level=0.95){
OR <- X[1,1]*X[2,2]/(X[1,2]*X[2,1])
logOR.SE <- sqrt(sum(1/X))
alpha <- 1 - conf.level
CI.lower <- exp(log(OR) - qnorm(1 - alpha/2)*logOR.SE)
CI.upper <- exp(log(OR) + qnorm(1 - alpha/2)*logOR.SE)
# qnorm returns the quantiles of a Gaussian distribution.
out <- list(OR=OR, CI=c(CI.lower, CI.upper), conf.level=conf.level)
return(out)
}
odds.ratio(X)
## $OR
## [1] 1.832054
##
## $CI
## [1] 1.440042 2.330780
##
## $conf.level
## [1] 0.95
## [1] 1.832054
odds.ratio <- function(X, conf.level=0.95){
cat("calculating odds ratio...","\n")
OR <- X[1,1]*X[2,2]/(X[1,2]*X[2,1])
logOR.SE <- sqrt(sum(1/X))
cat("calculating confidence interval...","\n")
alpha <- 1 - conf.level
CI.lower <- exp(log(OR) - qnorm(1 - alpha/2)*logOR.SE)
CI.upper <- exp(log(OR) + qnorm(1 - alpha/2)*logOR.SE)
cat("done, returning output...","\n")
out <- list(OR=OR, CI=c(CI.lower, CI.upper), conf.level=conf.level)
return(out)
}
out <- odds.ratio(X)
## calculating odds ratio...
## calculating confidence interval...
## done, returning output...
odds.ratio <- function(X, conf.level=0.95){
stopifnot(!missing(X), is.matrix(X),dim(X)==c(2,2),X>0)
cat("calculating odds ratio...","\n")
OR <- X[1,1]*X[2,2]/(X[1,2]*X[2,1])
logOR.SE <- sqrt(sum(1/X))
cat("calculating confidence interval...","\n")
alpha <- 1 - conf.level
CI.lower <- exp(log(OR) - qnorm(1 - alpha/2)*logOR.SE)
CI.upper <- exp(log(OR) + qnorm(1 - alpha/2)*logOR.SE)
cat("done, returning output...","\n")
out <- list(OR=OR, CI=c(CI.lower, CI.upper), conf.level=conf.level)
return(out)
}
out <- odds.ratio(X)
## calculating odds ratio...
## calculating confidence interval...
## done, returning output...
tryCatch function allows your code to be excuted even error exists
# result = tryCatch({
# expr
# }, warning = function(w) {
# warning-handler-code
# }, error = function(e) {
# error-handler-code
# }
# )
result = tryCatch({
2^6
}, warning = function(w) {
print(paste("MY_WARNING: ",w))
return("warning")
}, error = function(e) {
print(paste("MY_ERROR: ",e))
return("error")
}
)
result
## [1] 64
result = tryCatch({
1:3 + 1:2
}, warning = function(w) {
print(paste("MY_WARNING: ",w))
return("warning")
}, error = function(e) {
print(paste("MY_ERROR: ",e))
return("error")
}
)
## [1] "MY_WARNING: simpleWarning in 1:3 + 1:2: longer object length is not a multiple of shorter object length\n"
## [1] "warning"
result = tryCatch({
"A" + "B"
}, warning = function(w) {
print(paste("MY_WARNING: ",w))
return("warning")
}, error = function(e) {
print(paste("MY_ERROR: ",e))
return("error")
}
)
## [1] "MY_ERROR: Error in \"A\" + \"B\": non-numeric argument to binary operator\n"
## [1] "error"
Scoping is the set of rules that govern how R looks up the value of a symbol.
## [1] 3
## [1] 1
## [1] 1
## [1] 15
## [1] 15
## 1 2
## 1 2
## [1] 3
## [1] 3
## [1] 2
## [1] 1
## [,1] [,2]
## [1,] 22 49
## [2,] 28 64
## List of 2
## $ arg: num 2
## $ brg: num 10
## List of 2
## $ arg: num 2
## $ brg: num 10
## $arg
## [1] 2
##
## $brg
## [1] 10
2. then by prefix matching,
## $arg
## [1] 2
##
## $brg
## [1] 10
3. finally by position matching
## $arg
## [1] 2
##
## $brg
## [1] 10
## [1] 5.5
## [1] 5.5
## [1] 5.5
## [1] 5.5
## [1] 5.5
## [1] 5.5
## [1] 5.5
## [1] 5.5
## [1] 5.5
## [1] "/Users/zhuo/Dropbox (UFL)/teaching/2022FALL/lectures/Week2_Basic/functions"
## [1] "/Users/zhuo"
## [1] "/Users/zhuo/Dropbox (UFL)/teaching/2022FALL/lectures/Week2_Basic/functions"
## [1] "functions_files" "functions.html" "functions.R" "functions.rmd"
## [5] "tmp"
## [1] "Applications"
## [2] "boxplot_ratio.pdf"
## [3] "ClueGOConfiguration"
## [4] "CytoscapeConfiguration"
## [5] "DE_summary.csv"
## [6] "Desktop"
## [7] "Documents"
## [8] "Downloads"
## [9] "Dropbox"
## [10] "Dropbox (Personal)"
## [11] "Dropbox (UFL)"
## [12] "GD-2.50"
## [13] "httpsanalysis.ingenuity.compa_206442"
## [14] "kava_randomized.csv"
## [15] "Library"
## [16] "Movies"
## [17] "Music"
## [18] "my-ssh-key"
## [19] "my-ssh-key.pub"
## [20] "opt"
## [21] "perl5"
## [22] "Pictures"
## [23] "Public"
## [24] "PycharmProjects"
## [25] "scatter_miRNA_mitochondrial.pdf"
## [26] "Untitled1.ipynb"
## [27] "VocanoPlot_Control_X968_30nM.pdf"
## [28] "VocanoPlot_Control_X968_3nM.pdf"
## [29] "volcano_p<0.05.pdf"
## [30] "Zotero"
## [1] FALSE
## [1] FALSE
## Warning in dir.create("tmp"): 'tmp' already exists
## Warning in dir.create("tmp"): 'tmp' already exists
## [1] 50005000
## Time difference of 0.001522064 secs
## [1] 50005000
## Time difference of 0.003933907 secs
## [1] 2
## [1] 1
## function(x=4) x^2 + 1
## $x
## [1] 4
## x^2 + 1
## <environment: R_GlobalEnv>
Generally, an environment is similar to a list, with following important exceptions.
There are four special environments:
## [1] ".GlobalEnv" "package:stats" "package:graphics"
## [4] "package:grDevices" "package:utils" "package:datasets"
## [7] "package:methods" "Autoloads" "package:base"
## <environment: package:stats>
## attr(,"name")
## [1] "package:stats"
## attr(,"path")
## [1] "/Library/Frameworks/R.framework/Versions/4.0/Resources/library/stats"
## <environment: namespace:base>
User-defined function will be in the global environment, which will mask the function (of the same name) in the other loaded packages.
## [1] 1
## <environment: R_GlobalEnv>
## Package 'mclust' version 5.4.9
## Type 'citation("mclust")' for citing this R package in publications.
## [1] ".GlobalEnv" "package:mclust" "package:stats"
## [4] "package:graphics" "package:grDevices" "package:utils"
## [7] "package:datasets" "package:methods" "Autoloads"
## [10] "package:base"
## <environment: namespace:utils>
##
## Attaching package: 'combinat'
## The following object is masked from 'package:utils':
##
## combn
## [1] ".GlobalEnv" "package:combinat" "package:mclust"
## [4] "package:stats" "package:graphics" "package:grDevices"
## [7] "package:utils" "package:datasets" "package:methods"
## [10] "Autoloads" "package:base"
## <environment: namespace:combinat>
## <environment: namespace:utils>
## <environment: namespace:combinat>
## function (x)
## dim(x)[1L]
## <bytecode: 0x7fca200e04a8>
## <environment: namespace:base>
## [1] 2
## [1] 2 2
## [1] 2
## this is how R prevents external/global functions from affecting utilities functions inside a package
environment(nrow)
## <environment: namespace:base>
## <environment: R_GlobalEnv>
## function (x) .Primitive("dim")