diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d097271 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.Rhistory +Icon* +.RData diff --git a/1_DATASCITOOLBOX/Data Scientists Toolbox Course Notes.Rmd b/1_DATASCITOOLBOX/Data Scientists Toolbox Course Notes.Rmd index 8370812..22e87ba 100644 --- a/1_DATASCITOOLBOX/Data Scientists Toolbox Course Notes.Rmd +++ b/1_DATASCITOOLBOX/Data Scientists Toolbox Course Notes.Rmd @@ -20,13 +20,14 @@ $\pagebreak$ * `pwd` = print working directory (current directory) * `clear` = clear screen * `ls` = list stuff - * `-a` = see all (hidden) + * `-a` = see all (including hidden files) * `-l` = details * `cd` = change directory * `mkdir` = make directory * `touch` = creates an empty file * `cp` = copy * `cp ` = copy a file to a directory + * `cp ` = rename a file * `cp -r ` = copy all documents from directory to new Directory * `-r` = recursive * `rm` = remove @@ -102,7 +103,7 @@ $\pagebreak$ * **Big data** = now possible to collect data cheap, but not necessarily all useful (need the right data) ## Experimental Design -* Formulate you question in advance +* Formulate your question in advance * **Statistical inference** = select subset, run experiment, calculate descriptive statistics, use inferential statistics to determine if results can be applied broadly * ***[Inference]*** **Variability** = lower variability + clearer differences = decision * ***[Inference]*** **Confounding** = underlying variable might be causing the correlation (sometimes called Spurious correlation) @@ -118,5 +119,5 @@ $\pagebreak$ * **Accuracy** = Pr(correct outcome) * **Data dredging** = use data to fit hypothesis * **Good experiments** = have replication, measure variability, generalize problem, transparent -* Prediction is not inference, and be ware of data dredging +* Prediction is not inference, and beware of data dredging diff --git a/1_DATASCITOOLBOX/Data_Scientists_Toolbox_Course_Notes.html b/1_DATASCITOOLBOX/Data_Scientists_Toolbox_Course_Notes.html index 8ab13dd..ea78cf3 100644 --- a/1_DATASCITOOLBOX/Data_Scientists_Toolbox_Course_Notes.html +++ b/1_DATASCITOOLBOX/Data_Scientists_Toolbox_Course_Notes.html @@ -62,6 +62,7 @@

Xing Su

+

\(\pagebreak\)

CLI (Command Line Interface)

    @@ -71,7 +72,7 @@

    CLI (Command Line Interface)

  • clear = clear screen
  • ls = list stuff
      -
    • -a = see all (hidden)
    • +
    • -a = see all (including hidden files)
    • -l = details
  • cd = change directory
  • @@ -80,6 +81,7 @@

    CLI (Command Line Interface)

  • cp = copy
    • cp <file> <directory> = copy a file to a directory
    • +
    • cp <file> <renamed_file> = rename a file
    • cp -r <directory> <newDirectory> = copy all documents from directory to new Directory * -r = recursive
  • rm = remove @@ -169,7 +171,7 @@

    Data

    Experimental Design

      -
    • Formulate you question in advance
    • +
    • Formulate your question in advance
    • Statistical inference = select subset, run experiment, calculate descriptive statistics, use inferential statistics to determine if results can be applied broadly
    • [Inference] Variability = lower variability + clearer differences = decision
    • [Inference] Confounding = underlying variable might be causing the correlation (sometimes called Spurious correlation) @@ -191,7 +193,7 @@

      Experimental Design

  • Data dredging = use data to fit hypothesis
  • Good experiments = have replication, measure variability, generalize problem, transparent
  • -
  • Prediction is not inference, and be ware of data dredging
  • +
  • Prediction is not inference, and beware of data dredging
diff --git a/1_DATASCITOOLBOX/Data_Scientists_Toolbox_Course_Notes.pdf b/1_DATASCITOOLBOX/Data_Scientists_Toolbox_Course_Notes.pdf index 74595d8..c73220e 100644 Binary files a/1_DATASCITOOLBOX/Data_Scientists_Toolbox_Course_Notes.pdf and b/1_DATASCITOOLBOX/Data_Scientists_Toolbox_Course_Notes.pdf differ diff --git a/2_RPROG/R Programming Course Notes.Rmd b/2_RPROG/R Programming Course Notes.Rmd index 9a7ba87..d12a78c 100644 --- a/2_RPROG/R Programming Course Notes.Rmd +++ b/2_RPROG/R Programming Course Notes.Rmd @@ -360,7 +360,7 @@ $\pagebreak$ * ***examples*** * `apply(x, 1, sum)` or `apply(x, 1, mean)` = find row sums/means * `apply(x, 2, sum)` or `apply(x, 2, mean)` = find column sums/means - * `apply(x, 1, quantile, props = c(0.25, 0.75))` = find 25% 75% percentile of each row + * `apply(x, 1, quantile, probs = c(0.25, 0.75))` = find 25% 75% percentile of each row * `a <- array(rnorm(2*2*10), c(2, 2, 10))` = create 10 2x2 matrix * `apply(a, c(1, 2), mean)` = returns the means of 10 @@ -551,7 +551,7 @@ $\pagebreak$ ### Larger Tables * ***Note**: help page for read.table important* * need to know how much RAM is required $\rightarrow$ calculating memory requirements - * `numRow` x `numCol` x 8 bytes/numeric value = size required in bites + * `numRow` x `numCol` x 8 bytes/numeric value = size required in bytes * double the above results and convert into GB = amount of memory recommended * set `comment.char = ""` to save time if there are no comments in the file * specifying `colClasses` can make reading data much faster diff --git a/2_RPROG/R_Programming_Course_Notes.html b/2_RPROG/R_Programming_Course_Notes.html index 1bd6f9a..7bb80aa 100644 --- a/2_RPROG/R_Programming_Course_Notes.html +++ b/2_RPROG/R_Programming_Course_Notes.html @@ -139,6 +139,7 @@

Xing Su

+

\(\pagebreak\)

Overview and History of R

    @@ -195,6 +196,7 @@

    Overview and History of R

  • 5000+ packages available
+

\(\pagebreak\)

Coding Standards

@@ -239,6 +241,7 @@

R Console and Evaluation

  • [1] at the beginning of the output = which element of the vector is being shown
  • +

    \(\pagebreak\)

    R Objects and Data Structures

    @@ -374,7 +377,7 @@

    Matrices and Data Frames

  • data frames store tabular data
  • special type of list where every list has the same length (can be of different type)
  • data frames are usually created through read.table() and read.csv()
  • -
  • data.matrix() = converts a matrix to data frame
  • +
  • data.matrix() = converts a data frame to matrix.
  • colMeans(matrix) or rowMeans(matrix) = returns means of the columns/rows of a matrix/dataframe in a vector
  • as.numeric(rownames(df)) = returns row indices for rows of a data frame with unnamed rows
  • @@ -444,6 +447,7 @@

    Factors

  • table(factorVar) = how many of each are in the factor
  • +

    \(\pagebreak\)

    @@ -479,6 +483,7 @@

    Missing Values

  • Imputing Missing Values = replacing missing values with estimates (can be averages from all other data with the similar conditions)

  • +

    \(\pagebreak\)

    Sequence of Numbers

    @@ -576,6 +581,7 @@

    Partial Matching

  • x[["a", exact = false]]
  • +

    \(\pagebreak\)

    @@ -624,6 +630,7 @@

    Understanding Data

  • view(data.frame) = opens and view the content of the data frame
  • +

    \(\pagebreak\)

    Split-Apply-Combine Funtions

    @@ -671,7 +678,7 @@

    apply()

    • apply(x, 1, sum) or apply(x, 1, mean) = find row sums/means
    • apply(x, 2, sum) or apply(x, 2, mean) = find column sums/means
    • -
    • apply(x, 1, quantile, props = c(0.25, 0.75)) = find 25% 75% percentile of each row
    • +
    • apply(x, 1, quantile, probs = c(0.25, 0.75)) = find 25% 75% percentile of each row
    • a <- array(rnorm(2*2*10), c(2, 2, 10)) = create 10 2x2 matrix
    • apply(a, c(1, 2), mean) = returns the means of 10
    @@ -781,6 +788,7 @@

    aggregate()

  • na.rm = TRUE \(\rightarrow\) removes NA values
  • +

    \(\pagebreak\)

    @@ -874,6 +882,7 @@

    Generate Numbers for a Linear Model

    x <- rnorm(100)
     log.mu <- 0.5 + 0.3* x
     y <- rpois(100, exp(log.mu))
    +

    \(\pagebreak\)

    @@ -914,6 +923,7 @@

    Base Graphics

  • break = 100 = split data into 100 bins
  • +

    \(\pagebreak\)

    Reading Tabular Data

    @@ -950,7 +960,7 @@

    Larger Tables

  • Note: help page for read.table important
  • need to know how much RAM is required \(\rightarrow\) calculating memory requirements
      -
    • numRow x numCol x 8 bytes/numeric value = size required in bites
    • +
    • numRow x numCol x 8 bytes/numeric value = size required in bytes
    • double the above results and convert into GB = amount of memory recommended
  • set comment.char = "" to save time if there are no comments in the file
  • @@ -1001,6 +1011,7 @@

    Interfaces to the Outside World

    +

    \(\pagebreak\)

    @@ -1107,6 +1118,7 @@

    next and return

    } ## Do something here } +

    \(\pagebreak\)

    @@ -1161,6 +1173,7 @@

    Functions

  • Note: arguments coming after ... must be explicitly matched and cannot be partially matched
  • +

    \(\pagebreak\)

    Scoping

    @@ -1298,7 +1311,7 @@

    Optimization

    ## b <- -0.5*sum((data-mu)^2) / (sigma^2) ## -(a + b) ## } -## <environment: 0x7ff878f72bb8> +## <environment: 0x7fe5ce8c4f08>
    # Estimating Prameters
     optim(c(mu = 0, sigma = 1), nLL)$par
    ##       mu    sigma 
    @@ -1311,6 +1324,7 @@ 

    Optimization

    nLL <- make.NegLogLik(normals, c(1, FALSE)) optimize(nLL, c(1e-6, 10))$minimum
    ## [1] 1.800596
    +

    \(\pagebreak\)

    @@ -1365,7 +1379,7 @@

    R Profiler

    } })
    ##    user  system elapsed 
    -##   0.155   0.004   0.191
    +## 0.104 0.003 0.108
    • system.time(expression)
        diff --git a/2_RPROG/tips & tricks.Rmd b/2_RPROG/tips & tricks.Rmd new file mode 100644 index 0000000..592c0df --- /dev/null +++ b/2_RPROG/tips & tricks.Rmd @@ -0,0 +1,70 @@ +#### Compare a variable to a list of values +* find if a vlaue exists in a vector: +```{r} + +vec1 <- c(1,2,3,"abc","hab",123) +x <- 2 +print(is.element(x,vec1)) +[1] TRUE + +x <- 4 +print(is.element(x,vec1)) +[1] FALSE + +x <- c("abc",4) +print(is.element(x,vec1)) +[1] TRUE FALSE +``` + + +#### Subseting specific list of columns from a data frame +* subset() +```{r} +df <- read.csv("file.csv") +colnames(df) +[1] "c1" "c2" "c3" "c4" "c5" + +sub_df <- subset( + df,c1 == "abc" & !is.na(complete.cases(df[c2]), + select =c("c1","c2","c4")) +# this command will return specific columns of the df dataframe based on some conditions +# first the value in c1 should be equal to "abc" +# second the value in c2 should not be NA .. c2 should have value. +# if both conditions match then return the columns specified in the select statemnet +# if select not used will return all columns + +``` +#### Changing the names of a specific column/list of columns/all columns +```{r} +colnames(dataframe)[2] <- "column 3" +colnames(dataframe)[c(1,3,5)] <- c("Col 1","Col 3","Col 5") +colnames(dataframe)<-c("c1","c2","c3","c4","c5") +``` +#### Stop the warning message temporarly during the coercion process to avoid NA warnings + +* there are two options: +* option (1) +```{r} +#disable the warning message temporarly +options(warn = -1) +outcome_file[,outcome] <- as.numeric(outcome_file[,outcome]) +# enabling the warning messages again +options(warn = 0) +``` +* option (2) +```{r} +suppressWarnings(outcome_file[,outcome] <- as.numeric(outcome_file[,outcome])) + +``` +#### using which in subseting + +x[which(x$var2 >8), ] = returns the rows where the second column value is larger than 8 + + **Note*: which(condition) = useful in dealing with NA values as it returns the indices of the values where the condition holds true (returns FALSE for NA) * + + +#### Appending to a vector +#### loading a Vector into a Matrix by row. +#### invisible +#### time functions + diff --git a/3_GETDATA/Getting and Cleaning Data Course Notes.Rmd b/3_GETDATA/Getting and Cleaning Data Course Notes.Rmd index afc92d7..f7cfa68 100644 --- a/3_GETDATA/Getting and Cleaning Data Course Notes.Rmd +++ b/3_GETDATA/Getting and Cleaning Data Course Notes.Rmd @@ -2,13 +2,13 @@ title: "Getting and Cleaning Data Course Notes" author: "Xing Su" output: - pdf_document: - toc: yes - toc_depth: 3 html_document: highlight: pygments theme: spacelab toc: yes + pdf_document: + toc: yes + toc_depth: 3 --- diff --git a/3_GETDATA/Getting_and_Cleaning_Data_Course_Notes.html b/3_GETDATA/Getting_and_Cleaning_Data_Course_Notes.html index 3642274..83b5b32 100644 --- a/3_GETDATA/Getting_and_Cleaning_Data_Course_Notes.html +++ b/3_GETDATA/Getting_and_Cleaning_Data_Course_Notes.html @@ -80,6 +80,7 @@

        Xing Su

    +

    \(\pagebreak\)

    Overview

      @@ -138,6 +139,7 @@

      Tidy Data

    • in addition to script, possibly may need steps to run files, how script is run, and explicit instructions
    +

    \(\pagebreak\)

    Download files

    @@ -186,6 +188,7 @@

    Reading Excel files

  • XLConnect package has more options for writing/manipulating Excel files
  • generally good to store data in database/csv/tab separated files (.tab/.txt), easier to distribute
  • +

    \(\pagebreak\)

    Reading XML

    @@ -226,9 +229,10 @@

    Reading XML

  • extract content by attributes
    • doc <- htmlTreeParse(url, useInternal = True)
    • -
    • scores <- xpathSApply(doc, "//li@class='score'", xmlvalue) = look for li elements with class = "score" and return their value
    • +
    • scores <- xpathSApply(doc, "//li[@class='score']", xmlvalue) = look for li elements with class = "score" and return their value
  • +

    \(\pagebreak\)

    Reading JSON

    @@ -260,13 +264,14 @@

    Reading JSON

  • fromJSON() = converts from JSON object/code back to data frame
  • +

    \(\pagebreak\)

    data.table

    • inherits from data.frame (external package) \(\rightarrow\) all functions that accept data.frame work on data.table
    • can be much faster (written in C), much much faster at subsetting/grouping/updating
    • -
    • syntax: dt <- data.table(x = rnorm(9), y = rep(c(a, b, c), each = 3), z = rnorm(9)
    • +
    • syntax: dt <- data.table(x = rnorm(9), y = rep(c("a","b","c"), each = 3), z = rnorm(9))
    • tables() = returns all data tables in memory
      • shows name, nrow, MB, cols, key
      • @@ -278,7 +283,7 @@

        data.table

      • column subsetting (modified for data.table)
        • argument after comma is called an expression (collection of statements enclosed in {})
        • -
        • dt[, list(means(x), sum(z)] = returns mean of x column and sum of z column (no "" needed to specify column names, x and z in example)
        • +
        • dt[, list(mean(x), sum(z))] = returns mean of x column and sum of z column (no "" needed to specify column names, x and z in example)
        • dt[, table(y)] = get table of y value (perform any functions)
      • add new columns @@ -307,12 +312,12 @@

        data.table

      • .N = returns integer, length 1, containing the number (essentially count)
        • dt <- data.table (x=sample(letters[1:3], 1E5, TRUE)) = generates data table
        • -
        • dt[, .N by =x] = creates a table to count observations by the value of x
        • +
        • dt[, .N, by =x] = creates a table to count observations by the value of x
    • keys (quickly filter/subset)
        -
      • example: dt <- data.table(x = rep(c("a", "b", "c"), each 100), y = rnorm(300)) = generates data table +
      • example: dt <- data.table(x = rep(c("a", "b", "c"), each = 100), y = rnorm(300)) = generates data table
        • setkey(dt, x) = set the key to the x column
        • dt['a'] = returns a data frame, where x = ‘a’ (effectively filter)
        • @@ -329,14 +334,15 @@

          data.table

      • fast reading of files
          -
        • example: big_df <- data.frame(norm(1e6), norm(1e6)) = generates data table +
        • example: big_df <- data.frame(rnorm(1e6), rnorm(1e6)) = generates data table
          • file <- tempfile() = generates empty temp file
          • -
          • write.table(big.df, file=file, row.names=FALSE, col.names = TRUE, sep = "\t". quote = FALSE) = writes the generated data from big.df to the empty temp file
          • +
          • write.table(big_df, file=file, row.names=FALSE, col.names = TRUE, sep = "\t", quote = FALSE) = writes the generated data from big.df to the empty temp file
          • fread(file) = read file and load data = much faster than read.table()
      +

      \(\pagebreak\)

    Reading from MySQL

    @@ -345,7 +351,7 @@

    Reading from MySQL

  • free/widely used open sources database software, widely used for Internet base applications
  • each row = record
  • data are structured in databases \(\rightarrow\) series tables (dataset) \(\rightarrow\) fields (columns in dataset)
  • -
  • dbConnect(MySQL(), user = "genome", db = "hg19", host = "genome-mysql.cse.ucsc.edu) = open a connection to the database +
  • dbConnect(MySQL(), user = "genome", db = "hg19", host = "genome-mysql.cse.ucsc.edu") = open a connection to the database
    • db = "hg19" = select specific database
    • MySQL() can be replaced with other arguments to use other data structures
    • @@ -368,6 +374,7 @@

      Reading from MySQL

    • dbClearResult(query) = clears query from remote database, important
    • sqldf package example
    +

    \(\pagebreak\)

  • HDF5

    @@ -407,6 +414,7 @@

    HDF5

  • h5read("filename", "group/subgroupname/obj", index = list(1:3, 1)) = writes specified values in the specified obj at the group within the file
  • +

    \(\pagebreak\)

    Web Scraping (tutorial)

    @@ -448,6 +456,7 @@

    Web Scraping (tutorial +

    \(\pagebreak\)

    Working with API

    @@ -487,6 +496,7 @@

    Working with API

  • json1[json1$name == "datasharing",]$created_at = returns the create date for the data sharing repo
  • +

    \(\pagebreak\)

    Reading from Other Sources

    @@ -537,6 +547,7 @@

    Reading from Other Sources

    +

    \(\pagebreak\)

    dplyr

    @@ -640,6 +651,7 @@

    dplyr

    +

    \(\pagebreak\)

    tidyr

    @@ -684,6 +696,7 @@

    tidyr

  • Note: when there are redundant information, it’s better to split up the info into multiple tables; however, each table should also contain primary keys, which identify observations and link data from one table to the next
  • +

    \(\pagebreak\)

    lubridate

    @@ -717,6 +730,7 @@

    lubridate

  • with_tz(time, tone ="") = return date-time in a different time zone
  • as.period(new_interval(last_time, arrive)) = return the properly formatted difference between the two date times
  • +

    \(\pagebreak\)

    Subsetting and Sorting

    @@ -724,7 +738,7 @@

    Subsetting and Sorting

  • subsetting
    • x <- data.frame("var1" = sample(1:5), "var2" = sample(6:10), "var3" = (11:15)) = initiates a data frame with three names columns
    • -
    • x <- x[sample(1:5) = this scrambles the rows
    • +
    • x <- x[sample(1:5),] = this scrambles the rows
    • x$var2[c(2,3)] = NA = setting the 2nd and 3rd element of the second column to NA
    • x[1:2, "var2"] = subsetting the first two row of the the second column
    • x[(x$var1 <= 3 | x$var3 > 15), ] = return all rows of x where the first column is less than or equal to three or where the third column is bigger than 15
    • @@ -753,6 +767,7 @@

      Subsetting and Sorting

  • +

    \(\pagebreak\)

    Summarizing Data

    @@ -823,6 +838,7 @@

    Summarizing Data

  • print(object.size(obj), units = "Mb" = prints size of object in Mb
  • +

    \(\pagebreak\)

    Creating New Variables

    @@ -888,6 +904,7 @@

    Creating New Variables

  • exp(x) = exponential of x
  • +

    \(\pagebreak\)

    Reshaping Data

    @@ -936,6 +953,7 @@

    Reshaping Data

    +

    \(\pagebreak\)

    Merging Data

    @@ -957,6 +975,7 @@

    Merging Data

  • dfList = list(df1, df2, df3); join_all(dfList) = joins together a list of data frames using the common columns
  • +

    \(\pagebreak\)

    Editing Text Variables

    @@ -1015,6 +1034,7 @@

    Editing Text Variables

    +

    \(\pagebreak\)

    Regular Expressions

    @@ -1080,6 +1100,7 @@

    Regular Expressions

  • example: ([a-zA-Z]+) +\1 + matches “any word + at least one space + the same word repeated + at least one space” = “night night”, “so so”, etc.
  • +

    \(\pagebreak\)

    Working with Dates

    @@ -1119,6 +1140,7 @@

    Working with Dates

  • ?Sys.timezone = documentation on how to determine/set timezones
  • +

    \(\pagebreak\)

    Data Sources

    diff --git a/4_EXDATA/Exploratory Data Analysis Course Notes.Rmd b/4_EXDATA/Exploratory Data Analysis Course Notes.Rmd index 233b202..59b1408 100644 --- a/4_EXDATA/Exploratory Data Analysis Course Notes.Rmd +++ b/4_EXDATA/Exploratory Data Analysis Course Notes.Rmd @@ -27,8 +27,8 @@ $\pagebreak$ ```{r fig.height = 3, fig.width = 4, fig.align='center', echo=FALSE} # install grid and png packages if not present -library(png) -library(grid) +if (!require("pacman")) install.packages("pacman") +p_load("png", "grid") grid.raster(readPNG("figures/1.jpg")) ``` @@ -101,7 +101,8 @@ grid.raster(readPNG("figures/5.jpg")) ``` ```{r fig.height = 3, fig.width = 4, fig.align='center', message = F, warning = F, echo=FALSE} -library(UsingR); data(galton) +p_load("UsingR") +data(galton) plot(jitter(child, 4)~parent, galton) ``` @@ -167,7 +168,7 @@ grid.raster(readPNG("figures/6.jpg")) ### Base Plot Example ```{r fig.height = 4, fig.width = 6, fig.align='center'} -library(datasets) +p_load("datasets") # type =“n” sets up the plot and does not fill it with data with(airquality, plot(Wind, Ozone, main = "Ozone and Wind in New York City", type = "n")) # subsets of data are plotted here using different colors @@ -252,7 +253,7 @@ dev.off() $\pagebreak$ ## `lattice` Plotting System -* `library(lattice)` = load lattice system +* `p_load("lattice")` = load lattice system * implemented using the `lattice` and `grid` packages * `lattice` package = contains code for producing ***Trellis*** graphics (independent from base graphics system) * `grid` package = implements the graphing system; lattice build on top of grid @@ -297,7 +298,7 @@ $\pagebreak$ ### `lattice` Example ```{r fig.height = 4, fig.width = 6, fig.align='center'} -library(lattice) +p_load("lattice") set.seed(10) x <- rnorm(100) f <- rep(0:1, each = 50) @@ -318,7 +319,7 @@ xyplot(y ~ x | f, panel = function(x, y, ...) { $\pagebreak$ ## `ggplot2` Plotting System -* `library(ggplot2)` = loads ggplot2 package +* `p_load("ggplot2")` = loads ggplot2 package * implementation of Grammar of Graphics by Leland Wilkinson, written by Hadley Wickham (created RStudio) > "In brief, the grammar tells us that a statistical graphic is a mapping from data to aesthetic attributes (color, shape, size) of geometric objects (points, lines, bars). The plot may also contain statistical transformations of the data and is drawn on a specific coordinate system" @@ -347,7 +348,7 @@ $\pagebreak$ * ***example*** ```{r fig.height = 3, fig.width = 4, fig.align='center', message = F, warning = F} -library(ggplot2) +p_load("ggplot2") qplot(displ, hwy, data = mpg, color = drv, shape = drv) ``` @@ -783,7 +784,13 @@ plot(svd2$v[,2],pch=19,xlab="Column",ylab="Second right singular vector", + `k=10` = default value (take the nearest 10 rows) ```{r fig.height=3,fig.width=5, fig.align='center'} -library(impute) ## Available from http://bioconductor.org +## The "impute" library is available from http://bioconductor.org +## biocLite() is the recommended way to install Bioconductor packages. +## If the "impute" library is not already installed, you can uncomment +## the following lines to install it from Bioconductor. +#source("http://bioconductor.org/biocLite.R") +#biocLite(c("impute")) # install "impute" library +library(impute) data2 <- dataOrdered # set random samples = NA data2[sample(1:100,size=40,replace=FALSE)] <- NA @@ -875,7 +882,7 @@ plot(x, y, col = rgb(0, 0, 0, 0.2), main = "With Transparency") ### `RColorBrewer` Package * can be found on CRAN that has predefined color palettes - * `library(RColorBrewer)` + * `p_load("RColorBrewer")` * **types of palettes** - *Sequential* = numerical/continuous data that is ordered from low to high - *Diverging* = data that deviate from a value, increasing in two directions (i.e. standard deviations from the mean) @@ -895,7 +902,7 @@ grid.raster(readPNG("figures/color.png")) * ***example*** ```{r fig.height = 3, fig.width = 7, fig.align='center'} -library(RColorBrewer) +p_load("RColorBrewer") # generate 3 colors using brewer.pal function cols <- brewer.pal(3, "BuGn") pal <- colorRampPalette(cols) diff --git a/4_EXDATA/Exploratory_Data_Analysis_Course_Notes.html b/4_EXDATA/Exploratory_Data_Analysis_Course_Notes.html index ca8c602..1adecc7 100644 --- a/4_EXDATA/Exploratory_Data_Analysis_Course_Notes.html +++ b/4_EXDATA/Exploratory_Data_Analysis_Course_Notes.html @@ -126,6 +126,7 @@

    Xing Su

    +

    \(\pagebreak\)

    Principle of Analytic Graphics

      @@ -146,6 +147,7 @@

      Principle of Analytic Graphics

    • example
    +
    ## Loading required package: pacman

    • slightly negative relationship between pollution and mortality @@ -229,7 +231,7 @@

      Two Dimensional Summaries

    -

    +

    • multiple scatter plots
        @@ -315,7 +317,7 @@

        Base Graphics Functions and Parameters

    Base Plot Example

    -
    library(datasets)
    +
    p_load("datasets")
     # type =“n” sets up the plot and does not fill it with data
     with(airquality, plot(Wind, Ozone, main = "Ozone and Wind in New York City", type = "n"))
     # subsets of data are plotted here using different colors
    @@ -343,6 +345,7 @@ 

    Multiple Plot Example

    mtext("Ozone and Weather in New York City", outer = TRUE)} )

    +

    \(\pagebreak\)

    @@ -414,11 +417,12 @@

    Graphics Device

    dev.copy(png, file = "geyserplot.png") ## Don't forget to close the PNG device! dev.off()
    +

    \(\pagebreak\)

    lattice Plotting System

      -
    • library(lattice) = load lattice system
    • +
    • p_load("lattice") = load lattice system
    • implemented using the lattice and grid packages
      • lattice package = contains code for producing Trellis graphics (independent from base graphics system)
      • @@ -483,7 +487,7 @@

        lattice Functions and Parameters

    lattice Example

    -
    library(lattice)
    +
    p_load("lattice")
     set.seed(10)
     x <- rnorm(100)
     f <- rep(0:1, each = 50)
    @@ -499,12 +503,13 @@ 

    lattice Example

    panel.lmline(x, y, col = 2) })

    +

    \(\pagebreak\)

    ggplot2 Plotting System

      -
    • library(ggplot2) = loads ggplot2 package
    • +
    • p_load("ggplot2") = loads ggplot2 package
    • implementation of Grammar of Graphics by Leland Wilkinson, written by Hadley Wickham (created RStudio)
    @@ -542,7 +547,7 @@

    ggplot2 Functions and Parameters

  • example
  • -
    library(ggplot2)
    +
    p_load("ggplot2")
     qplot(displ, hwy, data = mpg, color = drv, shape = drv)

      @@ -677,6 +682,7 @@

      ggplot2 Comprehensive Example

      + labs(y = "Nocturnal Symptoms”) + labs(title = "MAACS Cohort”)

    Final Plot

    +

    \(\pagebreak\)

    @@ -701,6 +707,8 @@

    Hierarchical Clustering

    +
    ## Warning in readPNG("figures/25.png"): libpng warning: iCCP: known incorrect
    +## sRGB profile

    Procedure for Constructing Hierarchical Clusters (hclust function)

    @@ -812,7 +820,7 @@

    heatmap Function and Example

    set.seed(12345)
     data <- matrix(rnorm(400), nrow = 40)
     heatmap(data)
    -

    +

    image Function and Example

    @@ -839,6 +847,7 @@

    image Function and Example

    image(1:10, 1:40, t(data)[, nrow(data):1])

    +

    \(\pagebreak\)

    @@ -907,6 +916,7 @@

    Characteristics of K-means Clustering Algorithms

  • useful to run the algorithms a few times with different starting points to validate results
  • +

    \(\pagebreak\)

    @@ -1115,7 +1125,10 @@

    SVD and PCA Example

    -
    library(impute)  ## Available from http://bioconductor.org
    +
    #source("http://bioconductor.org/biocLite.R")
    +#biocLite() # install core packages
    +#biocLite(c("impute"), suppressUpdates=TRUE) # install "impute" library
    +library(impute) 
     data2 <- dataOrdered
     # set random samples = NA
     data2[sample(1:100,size=40,replace=FALSE)] <- NA
    @@ -1152,6 +1165,7 @@ 

    Create Approximations/Data Compression

    image(t(approx10)[,nrow(approx10):1], main = "10 Component") image(t(faceData)[,nrow(faceData):1], main = "Original")

    +

    \(\pagebreak\)

    @@ -1217,7 +1231,7 @@

    RColorBrewer Package

    • can be found on CRAN that has predefined color palettes
        -
      • library(RColorBrewer)
      • +
      • p_load("RColorBrewer")
    • types of palettes
        @@ -1241,7 +1255,7 @@

        RColorBrewer Package

    • example
    -
    library(RColorBrewer)
    +
    p_load("RColorBrewer")
     # generate 3 colors using brewer.pal function
     cols <- brewer.pal(3, "BuGn")
     pal <- colorRampPalette(cols)
    @@ -1265,6 +1279,7 @@ 

    RColorBrewer Package

    x <- rnorm(10000); y <- rnorm(10000)
     smoothScatter(x, y)

    +

    \(\pagebreak\)

    @@ -1386,6 +1401,7 @@

    Case Study: Human Activity Tracking with Smart Phones

    # plot first 10 centers of k-means for laying to understand which features drive the activity
     plot(kClust$center[4,1:10],pch=19,ylab="Cluster Center",xlab="")

    +

    \(\pagebreak\)

    Case Study: Fine Particle Pollution in the U.S. from 1999 to 2012

    diff --git a/4_EXDATA/Exploratory_Data_Analysis_Course_Notes.pdf b/4_EXDATA/Exploratory_Data_Analysis_Course_Notes.pdf index ee0506f..3beab1d 100644 Binary files a/4_EXDATA/Exploratory_Data_Analysis_Course_Notes.pdf and b/4_EXDATA/Exploratory_Data_Analysis_Course_Notes.pdf differ diff --git a/4_EXDATA/geyserplot.png b/4_EXDATA/geyserplot.png index 91f4f68..dac9908 100644 Binary files a/4_EXDATA/geyserplot.png and b/4_EXDATA/geyserplot.png differ diff --git a/5_REPDATA/Reproducible Research Course Notes.Rmd b/5_REPDATA/Reproducible Research Course Notes.Rmd index 1d3159f..7680b65 100644 --- a/5_REPDATA/Reproducible Research Course Notes.Rmd +++ b/5_REPDATA/Reproducible Research Course Notes.Rmd @@ -2,13 +2,13 @@ title: "Reproducible Research Course Notes" author: "Xing Su" output: - pdf_document: - toc: yes - toc_depth: 3 html_document: highlight: pygments theme: spacelab toc: yes + pdf_document: + toc: yes + toc_depth: 3 header-includes: \usepackage{graphicx} --- $\pagebreak$ @@ -31,8 +31,8 @@ $\pagebreak$ ```{r fig.height = 5, fig.width = 7, fig.align='center', echo=FALSE} # install grid and png packages if not present -library(png) -library(grid) +if (!require("pacman")) install.packages("pacman") +p_load("png", "grid") grid.raster(readPNG("figures/1.jpg")) ``` @@ -127,8 +127,8 @@ grid.raster(readPNG("figures/1.jpg")) * for the purpose of the SPAM question, data needs to be split into training and test test (Predictive) ```{r warning = F} -# If it isn't installed, install the kernlab package with install.packages() -library(kernlab) +# If it isn't installed, install the kernlab package with p_load() +p_load("kernlab") data(spam) set.seed(3435) trainIndicator = rbinom(4601, size = 1, prob = 0.5) @@ -174,7 +174,7 @@ grid.raster(readPNG("figures/3.jpg")) trainSpam$numType = as.numeric(trainSpam$type) - 1 costFunction = function(x, y) sum(x != (y > 0.5)) cvError = rep(NA, 55) -library(boot) +p_load("boot") for (i in 1:55) { # creates formula with one variable and the result lmFormula = reformulate(names(trainSpam)[i], response = "numType") @@ -363,8 +363,8 @@ $\pagebreak$ * `xtable`prints the table in html format, which is better presented than plain text normally ```{r showtable, results = "asis", fig.align = 'center'} -library(datasets) -library(xtable) +p_load("datasets") +p_load("xtable") fit <- lm(Ozone ~ Wind + Temp + Solar.R, data = airquality) xt <- xtable(summary(fit)) print(xt, "html") @@ -390,7 +390,7 @@ print(xt, "html") * alternatively, when not in RStudio, the process can be accomplished through the following ```r -library(knitr) +p_load("knitr") setwd() knit2html("document.Rmd") browseURL("document.html") @@ -643,7 +643,7 @@ grid.raster(readPNG("figures/4.png")) * ***example*** ```r -library(cacher) +p_load("cacher") clonecache(id = "092dcc7dda4b93e42f23e038a60e1d44dbec7b3f") clonecache(id = "092d") ## effectively the same as above # output: created cache directory '.cache' diff --git a/5_REPDATA/Reproducible_Research_Course_Notes.html b/5_REPDATA/Reproducible_Research_Course_Notes.html index 47bffb9..a266128 100644 --- a/5_REPDATA/Reproducible_Research_Course_Notes.html +++ b/5_REPDATA/Reproducible_Research_Course_Notes.html @@ -126,6 +126,7 @@

    Xing Su

    +

    \(\pagebreak\)

    Replication

      @@ -151,6 +152,7 @@

      Reproducibility

  • Research Pipeline
  • +
    ## Loading required package: pacman

    • Institute of Medicine report - Evolution of Translational Omics @@ -281,8 +283,8 @@

      Exploratory Data Analysis

      • for the purpose of the SPAM question, data needs to be split into training and test test (Predictive)
      -
      # If it isn't installed, install the kernlab package with install.packages()
      -library(kernlab)
      +
      # If it isn't installed, install the kernlab package with p_load()
      +p_load("kernlab")
       data(spam)
       set.seed(3435)
       trainIndicator = rbinom(4601, size = 1, prob = 0.5)
      @@ -343,7 +345,7 @@ 

      Statistical Modelling/Prediction

      trainSpam$numType = as.numeric(trainSpam$type) - 1
       costFunction = function(x, y) sum(x != (y > 0.5))
       cvError = rep(NA, 55)
      -library(boot)
      +p_load("boot")
       for (i in 1:55) {
           # creates formula with one variable and the result
           lmFormula = reformulate(names(trainSpam)[i], response = "numType")
      @@ -440,6 +442,7 @@ 

      Create Reproducible Code

  • ensure all analysis is reproducible (standard that most data analysis are judged on)
  • +

    \(\pagebreak\)

    @@ -615,13 +618,13 @@

    R Markdown

    -
    library(datasets)
    -library(xtable)
    +
    p_load("datasets")
    +p_load("xtable")
     fit <- lm(Ozone ~ Wind + Temp + Solar.R, data = airquality)
     xt <- xtable(summary(fit))
     print(xt, "html")
    - - + +
    @@ -711,7 +714,7 @@

    R Markdown

    • setting global options
        -
      • ```{r setoptions, echo = FALSE} opt_chunk$set(echo = FALSE, results = "hide")``` = sets the default option to not print the code/results unless otherwise specified
      • +
      • ```{r setoptions, echo = FALSE} opts_chunk$set(echo = FALSE, results = "hide")``` = sets the default option to not print the code/results unless otherwise specified
    • common options
        @@ -740,7 +743,7 @@

        R Markdown

      • alternatively, when not in RStudio, the process can be accomplished through the following
    -
    library(knitr)
    +
    p_load("knitr")
     setwd(<working directory>)
     knit2html("document.Rmd")
     browseURL("document.html")
    @@ -751,6 +754,7 @@

    R Markdown

  • Note: author should NOT edit/save the .md or .html document until you are done with the document
  • +

    \(\pagebreak\)

    Communicating Results

    @@ -788,6 +792,7 @@

    RPubs (link)

  • after kniting the R Markdown file in HTML, you can click on Publish to publish the HTML document on RPubs
  • Note: all publications to RPups are publicly available immediately
  • +

    \(\pagebreak\)

    @@ -916,6 +921,7 @@

    Don’ts

  • should save the data/code instead of the output
  • +

    \(\pagebreak\)

    @@ -1011,6 +1017,7 @@

    Evidence-based Data Analysis

  • gives reviewers an important tool without dramatically increasing the burden on them
  • allows more effort to be focused on improving the quality of “upstream” aspects of scientific research
  • +

    \(\pagebreak\)

    @@ -1104,7 +1111,7 @@

    cacher Package

  • example
  • -
    library(cacher)
    +
    p_load("cacher")
     clonecache(id = "092dcc7dda4b93e42f23e038a60e1d44dbec7b3f")
     clonecache(id = "092d")  ## effectively the same as above
     # output: created cache directory '.cache'
    @@ -1162,6 +1169,7 @@ 

    cacher Package

    # output: # / transferring cache db file 81b6dc23736f3d72c6... # [1] 0.000052457
    +

    \(\pagebreak\)

    @@ -1205,6 +1213,7 @@

    Conclusions and Lessons Learnt

  • reproducibility of NMMAPS allowed for a secondary analysis (and linking with PM chemical constituent data) investigating a novel hypothesis (Lippmann et al.), as well as a critique of that analysis and new findings (Dominici et al.)
  • original hypothesis not necessarily invalidated, but evidence not as strong as originally suggested (more work should be done)
  • +

    \(\pagebreak\)

    diff --git a/5_REPDATA/Reproducible_Research_Course_Notes.pdf b/5_REPDATA/Reproducible_Research_Course_Notes.pdf index c5669f6..89fe55b 100644 Binary files a/5_REPDATA/Reproducible_Research_Course_Notes.pdf and b/5_REPDATA/Reproducible_Research_Course_Notes.pdf differ diff --git a/6_STATINFERENCE/Statistical Inference Course Notes.Rmd b/6_STATINFERENCE/Statistical Inference Course Notes.Rmd index c573692..d08693c 100644 --- a/6_STATINFERENCE/Statistical Inference Course Notes.Rmd +++ b/6_STATINFERENCE/Statistical Inference Course Notes.Rmd @@ -2,13 +2,13 @@ title: "Statistical Inference Course Notes" author: "Xing Su" output: - pdf_document: - toc: yes - toc_depth: 3 html_document: highlight: pygments theme: spacelab toc: yes + pdf_document: + toc: yes + toc_depth: 3 word_document: default header-includes: \usepackage{graphicx} --- @@ -45,7 +45,9 @@ $\pagebreak$ * probability of the **union** of any two sets of outcomes that have nothing in common (mutually exclusive) = sum of respective probabilities ```{r echo = FALSE, fig.width = 4, fig.height = 3, fig.align = 'center'} -library(grid);library(png) +# install grid and png packages if not present +if (!require("pacman")) install.packages("pacman") +p_load("png", "grid") grid.raster(readPNG("figures/1.png")) ``` @@ -239,7 +241,9 @@ $\pagebreak$ ```{r fig.width = 5, fig.height = 3, fig.align = 'center', message = F, warning = F} # load relevant packages -library(UsingR); data(galton); library(ggplot2) +p_load("UsingR") +data(galton) +p_load("ggplot2") # plot galton data g <- ggplot(galton, aes(x = child)) # add histogram for children data @@ -440,7 +444,8 @@ sd(apply(matrix(sample(0 : 1, nosim * n, replace = TRUE), nosim), 1, mean)) ```{r fig.width = 4, fig.height = 3, fig.align = 'center', message = F, warning = F} # load data -library(UsingR); data(father.son); +p_load("UsingR") +data(father.son); # define son height as the x variable x <- father.son$sheight # n is the length @@ -612,7 +617,7 @@ $\pagebreak$ ```{r fig.width = 6, fig.height = 3, fig.align = 'center', message = F, warning = F} # load library -library(gridExtra) +p_load("gridExtra") # specify number of trials n <- 10000 # calculate sample (from normal distribution) means for different size of n @@ -1108,7 +1113,7 @@ $\pagebreak$ * Power increases as **n** increases $\rightarrow$ with more data, more likely to detect any alternative $mu_a$ ```{r, fig.align='center', fig.height=4, fig.width=6} -library(ggplot2) +p_load("ggplot2") mu0 = 30; mua = 32; sigma = 4; n = 16 alpha = 0.05 z = qnorm(1 - alpha) @@ -1120,7 +1125,7 @@ power = sapply(nseq, function(n) ) colnames(power) <- paste("n", nseq, sep = "") d <- data.frame(mu_a, power) -library(reshape2) +p_load("reshape2") d2 <- melt(d, id.vars = "mu_a") names(d2) <- c("mu_a", "n", "power") g <- ggplot(d2, @@ -1265,7 +1270,8 @@ $\pagebreak$ ```{r fig.align='center', fig.height=4, fig.width=6, message = F, warning = F} # load data -library(UsingR); data(father.son) +p_load("UsingR") +data(father.son) # observed dataset x <- father.son$sheight # number of simulated statistic diff --git a/6_STATINFERENCE/Statistical_Inference_Course_Notes.html b/6_STATINFERENCE/Statistical_Inference_Course_Notes.html index 495a54c..4561c2e 100644 --- a/6_STATINFERENCE/Statistical_Inference_Course_Notes.html +++ b/6_STATINFERENCE/Statistical_Inference_Course_Notes.html @@ -142,6 +142,7 @@

    Xing Su

    +

    \(\pagebreak\)

    Overview

      @@ -191,6 +192,7 @@

      General Probability Rules

    • probability of something = 1 - probability of the opposite occurring
    • probability of the union of any two sets of outcomes that have nothing in common (mutually exclusive) = sum of respective probabilities
    +
    ## Loading required package: pacman

    • if \(A\) implies occurrence of \(B\), then \(P(A)\) occurring \(< P(B)\) occurring
    • @@ -222,6 +224,7 @@

      Baye’s Rule

      • definition \[P(B\:|\:A) = \frac{P(A\:|\:B)P(B)}{P(A\:|\:B)P(B)+P(A\:|\:B^c)P(B^c)}\] where \(B^c\) = corresponding probability of event \(B\), \(P(B^c) = 1 - P(B)\)
      +

      \(\pagebreak\)

    @@ -367,6 +370,7 @@

    IID Random Variables

  • IID random variables = default model for random samples = default starting point of inference
  • +

    \(\pagebreak\)

    @@ -415,6 +419,7 @@

    Likelihood Ratios

  • this indicates that the result of the positive test is the odds of disease is 66 times the pretest odds
  • +

    \(\pagebreak\)

    @@ -453,7 +458,9 @@

    Expected Values/Mean

    # load relevant packages
    -library(UsingR); data(galton); library(ggplot2)
    +p_load("UsingR")
    +data(galton)
    +p_load("ggplot2")
     # plot galton data
     g <- ggplot(galton, aes(x = child))
     # add histogram for children data
    @@ -496,7 +503,8 @@ 

    Expected Values/Mean

    # plot histograms of means by sample size g <- ggplot(dat, aes(x = x, fill = size)) + geom_histogram(alpha = .20, binwidth=.25, colour = "black") g + facet_grid(. ~ size)
    -

    +

    +

    \(\pagebreak\)

    Variance

    @@ -562,7 +570,7 @@

    Sample Variance

    # plot density function for different sample size data ggplot(dat, aes(x = x, fill = n)) + geom_density(size = 1, alpha = .2) + geom_vline(xintercept = 1, size = 1)
    -

    +

    • Note: for any variable, properties of the population = parameter, estimates of properties for samples = statistic
        @@ -634,7 +642,7 @@

        Example - Standard Normal

        nosim <- 1000; n <-10 # estimated standard deviation of mean sd(apply(matrix(rnorm(nosim * n), nosim), 1, mean))
        -
        ## [1] 0.3144349
        +
        ## [1] 0.3186217
        # actual standard deviation of mean of standard normals
         1 / sqrt(n)
        ## [1] 0.3162278
        @@ -653,7 +661,7 @@

        Example - Standard Uniform

      # estimated standard deviation of the sample means
       sd(apply(matrix(runif(nosim * n), nosim), 1, mean))
      -
      ## [1] 0.08643882
      +
      ## [1] 0.09083247
      # actual standard deviation of the means
       1/sqrt(12*n)
      ## [1] 0.09128709
      @@ -666,7 +674,7 @@

      Example - Poisson

    # estimated standard deviation of the sample means
     sd(apply(matrix(rpois(nosim * n, lambda=4), nosim), 1, mean))
    -
    ## [1] 0.6213867
    +
    ## [1] 0.6041711
    # actual standard deviation of the means
     2/sqrt(n)
    ## [1] 0.6324555
    @@ -679,7 +687,7 @@

    Example - Bernoulli

    # estimated standard deviation of the sample means
     sd(apply(matrix(sample(0 : 1, nosim * n, replace = TRUE), nosim), 1, mean))
    -
    ## [1] 0.1573651
    +
    ## [1] 0.1592639
    # actual standard deviation of the means
     1/(2*sqrt(n))
    ## [1] 0.1581139
    @@ -687,7 +695,8 @@

    Example - Bernoulli

    Example - Father/Son

    # load data
    -library(UsingR); data(father.son);
    +p_load("UsingR")
    +data(father.son);
     # define son height as the x variable
     x <- father.son$sheight
     # n is the length
    @@ -706,6 +715,7 @@ 

    Example - Father/Son

    sampleMeanSd = sd(x) / sqrt(n)),2)
    ##     sampleVar sampleMeanVar      sampleSd  sampleMeanSd 
     ##          7.92          0.01          2.81          0.09
    +

    \(\pagebreak\)

    @@ -747,6 +757,7 @@

    Example

  • lower.tail=FALSE = returns the complement, in this case it’s the probability of greater than 6 successes out of 8 samples with probability of 0.5
  • +

    \(\pagebreak\)

    @@ -808,6 +819,7 @@

    Example

    • therefore, 1053.7244875 would represent the threshold that has more clicks than 75% of days
    +

    \(\pagebreak\)

    @@ -865,6 +877,7 @@

    Example - Approximating Binomial Distribution

    • as we can see from above, the two probabilities (12.3385774% vs 12.3385774%) are extremely close
    +

    \(\pagebreak\)

    @@ -893,7 +906,7 @@

    Example - LLN for Normal and Bernoulli Distribution

  • we will plot the distribution of sample means as \(n\) increases and compare it to the population means
  • # load library
    -library(gridExtra)
    +p_load("gridExtra")
     # specify number of trials
     n <- 10000
     # calculate sample (from normal distribution) means for different size of n
    @@ -912,7 +925,7 @@ 

    Example - LLN for Normal and Bernoulli Distribution

    p <- p + ggtitle("Bernoulli Distribution (Coin Flip)") # combine plots grid.arrange(g, p, ncol = 2)
    -

    +

    • as we can see from above, for both distributions the sample means undeniably approach the respective population means as \(n\) increases
    @@ -944,11 +957,11 @@

    Example - CLT with Bernoulli Trials (Coin Flips)

  • in principle, normalizing the random variable \(X_i\), we should get an approximately standard normal distribution \[\frac{\hat p - p}{\sqrt{p(1-p)/n}} \sim N(0,~1)\]
  • therefore, we will flip a coin \(n\) times, take the sample proportion of heads (successes with probability \(p\)), subtract off 0.5 (ideal sample proportion) and multiply the result by \(\frac{1}{2 \sqrt{n}}\) and compare it to the standard normal
  • -

    +

    • now, we can run the same simulation trials for an extremely unfair coin with \(p\) = 0.9
    -

    +

    • as we can see from both simulations, the converted/standardized distribution of the samples convert to the standard normal distribution
    • Note: speed at which the normalized coin flips converge to normal distribution depends on how biased the coin is (value of \(p\))
    • @@ -1041,7 +1054,7 @@

      Confidence Interval - Binomial Distribution/Agresti-Coull Interval

      }) # plot CI results vs 95% ggplot(data.frame(pvals, coverage), aes(x = pvals, y = coverage)) + geom_line(size = 2) + geom_hline(yintercept = 0.95) + ylim(.75, 1.0) -

      +

      • as we can see from above, the interval do not provide adequate coverage as 95% confidence intervals (frequently only provide 80 to 90% coverage)
      • we can construct the Agresti-Coull Interval, which is defined uses the adjustment \[\hat{p} = \frac{X+2}{n+4}\] where we effectively add 2 to number of successes, \(X\), and add 2 to number of failure
      • @@ -1066,7 +1079,7 @@

        Confidence Interval - Binomial Distribution/Agresti-Coull Interval

        }) # plot CI results vs 95% ggplot(data.frame(pvals, coverage), aes(x = pvals, y = coverage)) + geom_line(size = 2) + geom_hline(yintercept = 0.95) + ylim(.75, 1.0) -

        +

        • as we can see from above, the coverage is much better for the 95% interval
        • in fact, all of the estimates are more conservative as we previously discussed, indicating the Agresti-Coull intervals are wider than the regular confidence intervals
        • @@ -1117,7 +1130,7 @@

          Confidence Interval - Poisson Interval

          }) # plot CI results vs 95% ggplot(data.frame(lambdavals, coverage), aes(x = lambdavals, y = coverage)) + geom_line(size = 2) + geom_hline(yintercept = 0.95)+ylim(0, 1.0) -

          +

          • as we can see above, for small values of \(\lambda = X/t\), the confidence interval produced by the asymptotic interval is not an accurate estimate of the actual 95% interval (not enough coverage)
          • however, as \(t \to \infty\), the interval becomes the true 95% interval
          • @@ -1137,7 +1150,7 @@

            Confidence Interval - Poisson Interval

            }) # plot CI results vs 95% ggplot(data.frame(lambdavals, coverage), aes(x = lambdavals, y = coverage)) + geom_line(size = 2) + geom_hline(yintercept = 0.95) + ylim(0, 1.0) -

            +

            • as we can see from above, as \(t\) increases, the Poisson intervals become closer to the actual 95% confidence intervals
            @@ -1317,6 +1330,7 @@

            Independent Group t Intervals - Different Variance

      +

      \(\pagebreak\)

    @@ -1487,6 +1501,7 @@

    Hypothesis Testing

    +

    \(\pagebreak\)

    Power

    @@ -1523,7 +1538,7 @@

    Power

    -
    library(ggplot2)
    +
    p_load("ggplot2")
     mu0 = 30; mua = 32; sigma = 4; n = 16
     alpha = 0.05
     z = qnorm(1 - alpha)
    @@ -1535,7 +1550,7 @@ 

    Power

    ) colnames(power) <- paste("n", nseq, sep = "") d <- data.frame(mu_a, power) -library(reshape2) +p_load("reshape2") d2 <- melt(d, id.vars = "mu_a") names(d2) <- c("mu_a", "n", "power") g <- ggplot(d2, @@ -1587,6 +1602,7 @@

    Power

    +

    \(\pagebreak\)

    Multiple Testing

    @@ -1729,6 +1745,7 @@

    Example

    ## not zero zero ## FALSE 0 487 ## TRUE 500 13
    +

    \(\pagebreak\)

    @@ -1757,7 +1774,8 @@

    Resample Inference

    # load data
    -library(UsingR); data(father.son)
    +p_load("UsingR")
    +data(father.son)
     # observed dataset
     x <- father.son$sheight
     # number of simulated statistic
    diff --git a/6_STATINFERENCE/Statistical_Inference_Course_Notes.pdf b/6_STATINFERENCE/Statistical_Inference_Course_Notes.pdf
    index 0f02711..e3c676d 100644
    Binary files a/6_STATINFERENCE/Statistical_Inference_Course_Notes.pdf and b/6_STATINFERENCE/Statistical_Inference_Course_Notes.pdf differ
    diff --git a/7_REGMODS/Regression Models Course Notes.Rmd b/7_REGMODS/Regression Models Course Notes.Rmd
    index 16539ed..08f6a89 100644
    --- a/7_REGMODS/Regression Models Course Notes.Rmd	
    +++ b/7_REGMODS/Regression Models Course Notes.Rmd	
    @@ -2,11 +2,11 @@
     title: "Regression Models Course Notes"
     author: "Xing Su"
     output:
    +  html_document:
    +    toc: yes
       pdf_document:
         toc: yes
         toc_depth: 3
    -  html_document:
    -    toc: yes
     header-includes: \usepackage{graphicx} \usepackage{color} \usepackage{mathtools}
     ---
     $\pagebreak$
    @@ -84,8 +84,10 @@ $\pagebreak$
     	* ***Note**: `manipulate` function can help to show this *
     
     ```{r fig.height = 2.5, fig.width = 7, message = FALSE, fig.align = 'center'}
    -# load necessary packages/install if needed
    -library(ggplot2); library(UsingR); data(galton)
    +# install grid and png packages if not present
    +if (!require("pacman")) install.packages("pacman")
    +p_load("ggplot2", "UsingR")
    +data(galton)
     # function to plot the histograms
     myHist <- function(mu){
     	# calculate the mean squares
    @@ -99,7 +101,7 @@ myHist <- function(mu){
         g
     }
     # manipulate allows the user to change the variable mu to see how the mean squares changes
    -#   library(manipulate); manipulate(myHist(mu), mu = slider(62, 74, step = 0.5))]
    +#   p_load("manipulate"); manipulate(myHist(mu), mu = slider(62, 74, step = 0.5))]
     # plot the correct graph
     myHist(mean(galton$child))
     ```
    @@ -109,7 +111,7 @@ myHist(mean(galton$child))
     * ***Note**: because there are multiple data points for the same parent/child combination, a third dimension (size of point) should be used when constructing the scatter plot *
     
     ```{r fig.height = 3.5,fig.width = 5, fig.align='center', message = FALSE}
    -library(dplyr)
    +p_load("dplyr")
     # constructs table for different combination of parent-child height
     freqData <- as.data.frame(table(galton$child, galton$parent))
     names(freqData) <- c("child (in)", "parent (in)", "freq")
    @@ -163,7 +165,8 @@ $\pagebreak$
     
     ```{r fig.height = 2.5, fig.width = 3, fig.align='center', echo=FALSE, message = FALSE}
     # install grid and png packages if not present
    -library(png); library(grid); doMC::registerDoMC(cores = 4)
    +p_load("png", "grid", "doMC")
    +doMC::registerDoMC(cores = 4)
     grid.raster(readPNG("figures/1.png"))
     ```
     
    @@ -1017,7 +1020,9 @@ coef(lm(y ~ x + x2 + x3))
     
     ```{r fig.height=6.5, fig.width=7, fig.align = 'center', warning = F, message = F}
     # load dataset
    -require(datasets); data(swiss); require(GGally)
    +p_load("datasets")
    +data(swiss)
    +p_load("GGally")
     # produce pairwise plot using ggplot2
     ggpairs(swiss, lower = list(continuous = "smooth"),params = c(method = "loess"))
     # print coefficients of regression of fertility on all predictors
    @@ -2030,7 +2035,7 @@ c(summary(fit2)$cov.unscaled[2,2], summary(fit3)$cov.unscaled[2,2]) / a - 1
     
     ```{r message = F, warning = F}
     # load car library
    -library(car)
    +p_load("car")
     # run linear regression on Fertility vs all other predictors
     fit <- lm(Fertility ~ . , data = swiss)
     # calculate the variance inflation factors
    @@ -2301,7 +2306,7 @@ g(\mu_i) & = \log \left(\frac{\mu_i}{1 - \mu_i} \right) = \eta_i\\
     ```r
     # set x values for the points to be plotted
     x <- seq(-10, 10, length = 1000)
    -# "library(manipulate)" is needed to use the manipulate function
    +# "p_load("manipulate")" is needed to use the manipulate function
     manipulate(
     	# plot the logistic regression curve
         plot(x, exp(beta0 + beta1 * x) / (1 + exp(beta0 + beta1 * x)),
    @@ -2517,7 +2522,7 @@ plot(glm1$fitted,glm1$residuals,pch=19,col="grey",ylab="Residuals",xlab="Fitted"
     
     ```{r message = F, warning = F}
     # load sandwich package
    -library(sandwich)
    +p_load("sandwich")
     # compute
     confint.agnostic <- function (object, parm, level = 0.95, ...)
     {
    diff --git a/7_REGMODS/Regression_Models_Course_Notes.html b/7_REGMODS/Regression_Models_Course_Notes.html
    index 80277b0..6ac2052 100644
    --- a/7_REGMODS/Regression_Models_Course_Notes.html
    +++ b/7_REGMODS/Regression_Models_Course_Notes.html
    @@ -184,6 +184,7 @@ 

    Xing Su

    +

    \(\pagebreak\)

    Introduction to Regression

      @@ -274,6 +275,7 @@

      Empirical Covariance & Correlation

    • \(Cor(X, Y) = 0\) implies no linear relationship
    +

    \(\pagebreak\)

    @@ -293,8 +295,10 @@

    Dalton’s Data and Least Squares

  • Note: manipulate function can help to show this
  • -
    # load necessary packages/install if needed
    -library(ggplot2); library(UsingR); data(galton)
    +
    # install grid and png packages if not present
    +if (!require("pacman")) install.packages("pacman")
    +p_load("ggplot2", "UsingR")
    +data(galton)
     # function to plot the histograms
     myHist <- function(mu){
         # calculate the mean squares
    @@ -308,7 +312,7 @@ 

    Dalton’s Data and Least Squares

    g } # manipulate allows the user to change the variable mu to see how the mean squares changes -# library(manipulate); manipulate(myHist(mu), mu = slider(62, 74, step = 0.5))] +# p_load("manipulate"); manipulate(myHist(mu), mu = slider(62, 74, step = 0.5))] # plot the correct graph myHist(mean(galton$child))

    @@ -316,7 +320,7 @@

    Dalton’s Data and Least Squares

  • in order to visualize the parent-child height relationship, a scatter plot can be used

  • Note: because there are multiple data points for the same parent/child combination, a third dimension (size of point) should be used when constructing the scatter plot

  • -
    library(dplyr)
    +
    p_load("dplyr")
     # constructs table for different combination of parent-child height
     freqData <- as.data.frame(table(galton$child, galton$parent))
     names(freqData) <- c("child (in)", "parent (in)", "freq")
    @@ -333,7 +337,8 @@ 

    Dalton’s Data and Least Squares

    # change the color gradient from default to lightblue -> $white g <- g + scale_colour_gradient(low = "lightblue", high="white") g
    -

    +

    +

    \(\pagebreak\)

    Derivation for Least Squares = Empirical Mean (Finding the Minimum)

      @@ -360,6 +365,7 @@

      Derivation for Least Squares = Empirical Mean (Finding the Minimum)

      \end{aligned} \]
    +

    \(\pagebreak\)

    @@ -407,6 +413,7 @@

    Derivation for \(\beta\)

  • Note: this is the result from our previous derivation for least squares = empirical mean
  • +

    \(\pagebreak\)

    @@ -546,7 +553,8 @@

    Examples and R Commands

    g <- g + scale_colour_gradient(low = "lightblue", high="white") g <- g + geom_smooth(method="lm", formula=y~x) g
    -

    +

    +

    \(\pagebreak\)

    @@ -617,6 +625,7 @@

    Dalton’s Investigation on Regression to the Mean

    geom_text(x = 1.6, y = 3.8, label="y~x", angle = 60) g

    +

    \(\pagebreak\)

    @@ -782,6 +791,7 @@

    Example and R Commands

    # add text labels text(newx+c(0.03, 0, 0), rep(250, 3), labels = newx, pos = 2)

    +

    \(\pagebreak\)

    Derivation for Maximum Likelihood Estimator

    @@ -828,6 +838,7 @@

    Derivation for Maximum Likelihood Estimator

    \end{aligned} \] +

    \(\pagebreak\)

    @@ -1009,7 +1020,7 @@

    Example and R Commands

    geom_point(size = 2, colour = "red", alpha = 0.4)+ xlab("X") + ylab("Residual")+ ggtitle("Residual vs X") multiplot(g, f, cols = 2)
    -

    +

    • heteroskedasticity = heteroskedastic model’s variance is not constant and is a function of x
    @@ -1029,7 +1040,8 @@

    Example and R Commands

    xlab("X") + ylab("Residual") + ggtitle("Residual vs X") # combine two plots multiplot(g, f, cols = 2) -

    +

    +

    \(\pagebreak\)

    @@ -1203,6 +1215,7 @@

    Prediction Interval

    lines(xVals,yVals+2*se1); lines(xVals,yVals-2*se1) lines(xVals,yVals+2*se2); lines(xVals,yVals-2*se2)

    +

    \(\pagebreak\)

    @@ -1328,15 +1341,15 @@

    Example: Linear Model with 2 Variables and Intercept

    ex = resid(lm(x ~ x2 + x3)) # estimate beta1 for var1 sum(ey * ex) / sum(ex ^ 2) -
    ## [1] 1.000587
    +
    ## [1] 1.007277
    # regression through the origin with xva1 with var2 var3 effect removed
     coef(lm(ey ~ ex - 1))
    ##       ex 
    -## 1.000587
    +## 1.007277
    # regression for all three variables
     coef(lm(y ~ x + x2 + x3))
    ## (Intercept)           x          x2          x3 
    -##   1.0078126   1.0005866   0.9791919   1.0083305
    +## 0.9923629 1.0072773 0.9937988 0.9902259

    Example: Coefficients that Reverse Signs

    @@ -1355,7 +1368,9 @@

    Example: Coefficients that Reverse Signs

  • [GGally package] ggpairs(data) = produces pair wise plot for the predictors similar to pairs in base package
  • # load dataset
    -require(datasets); data(swiss); require(GGally)
    +p_load("datasets")
    +data(swiss)
    +p_load("GGally")
     # produce pairwise plot using ggplot2
     ggpairs(swiss, lower = list(continuous = "smooth"),params = c(method = "loess"))

    @@ -1398,12 +1413,12 @@

    Example: Coefficients that Reverse Signs

    c("with x1" = summary(lm(y ~ x1))$coef[2,1], "with x1 and x2" = summary(lm(y ~ x1 + x2))$coef[2,1])
    ##        with x1 with x1 and x2 
    -##     96.2686728     -0.9878422
    +## 94.2505634 -0.9851072
    # print p-values
     c("with x1" = summary(lm(y ~ x1))$coef[2,4],
         "with x1 and x2" = summary(lm(y ~ x1 + x2))$coef[2,4])
    ##        with x1 with x1 and x2 
    -##   1.910394e-75   5.215443e-69
    +## 8.283145e-72 2.902098e-74
    # store all data in one data frame (ey and ex1 are residuals with respect to x2)
     dat <- data.frame(y = y, x1 = x1, x2 = x2, ey = resid(lm(y ~ x2)), ex1 = resid(lm(x1 ~ x2)))
     # plot y vs x1
    @@ -1419,7 +1434,7 @@ 

    Example: Coefficients that Reverse Signs

    y = "resid(y~x2)") # combine plots multiplot(g, g2, cols = 2)
    -

    +

    • as we can see from above, the correlation between y and x1 flips signs when adjusting for x2
    • this effectively means that within each consecutive group/subset of points (each color gradient) on the left hand plot (unadjusted), there exists a negative relationship between the points while the overall trend is going up

    • @@ -1452,6 +1467,7 @@

      Example: Unnecessary Variables

      • as we can see above, the R dropped the unnecessary variable z by excluding it from the linear regression \(\rightarrow\) z’s coefficient is NA
      +

      \(\pagebreak\)

    @@ -1611,6 +1627,7 @@

    Example: 6 Factor Level Insect Spray Data

  • Poisson GLMs are better (don’t have to worry about the assumptions) for fitting count data
  • +

    \(\pagebreak\)

    @@ -1804,7 +1821,8 @@

    Example: % Hungry ~ Year + Income + Year * Income (Continuous Interaction)
    ##   (Intercept)          Year        Income   Year:Income 
    -##  1.019197e+03 -5.009142e-01 -3.346212e-02  1.674339e-05
    +## 7.935700e+02 -3.882114e-01 -1.358743e-02 6.815886e-06 +

    \(\pagebreak\)

    @@ -1846,12 +1864,12 @@

    Simulation 1 - Treatment = Adjustment Effect

    # add in the actual data points points(x[1 : (n/2)], y[1 : (n/2)], pch = 21, col = "black", bg = "lightblue", cex = 1) points(x[(n/2 + 1) : n], y[(n/2 + 1) : n], pch = 21, col = "black", bg = "salmon", cex = 1) -

    +

    # print treatment and adjustment effects
     rbind("Treatment Effect" = lm(y~t+x)$coef[2], "Adjustment Effect" = lm(y~t)$coef[2])
    -
    ##                           t
    -## Treatment Effect  1.0158044
    -## Adjustment Effect 0.8934351
    +
    ##                          t
    +## Treatment Effect  1.032922
    +## Adjustment Effect 1.042037
    • in the above graph, the elements are as follows:
        @@ -1902,12 +1920,12 @@

        Simulation 2 - No Treatment Effect

        # add in the actual data points points(x[1 : (n/2)], y[1 : (n/2)], pch = 21, col = "black", bg = "lightblue", cex = 1) points(x[(n/2 + 1) : n], y[(n/2 + 1) : n], pch = 21, col = "black", bg = "salmon", cex = 1) -

        +

        # print treatment and adjustment effects
         rbind("Treatment Effect" = lm(y~t+x)$coef[2], "Adjustment Effect" = lm(y~t)$coef[2])
        -
        ##                            t
        -## Treatment Effect  -0.1230508
        -## Adjustment Effect  2.9541291
        +
        ##                             t
        +## Treatment Effect  -0.04770288
        +## Adjustment Effect  3.08437957
        • in the above graph, the elements are as follows:
            @@ -1977,12 +1995,12 @@

            Simulation 3 - Treatment Reverses Adjustment Effect

            # add in the actual data points points(x[1 : (n/2)], y[1 : (n/2)], pch = 21, col = "black", bg = "lightblue", cex = 1) points(x[(n/2 + 1) : n], y[(n/2 + 1) : n], pch = 21, col = "black", bg = "salmon", cex = 1) -

            +

            # print treatment and adjustment effects
             rbind("Treatment Effect" = lm(y~t+x)$coef[2], "Adjustment Effect" = lm(y~t)$coef[2])
            -
            ##                            t
            -## Treatment Effect  -1.0932789
            -## Adjustment Effect  0.9830358
            +
            ##                           t
            +## Treatment Effect  -1.117413
            +## Adjustment Effect  0.850717
            • in the above graph, the elements are as follows:
                @@ -2026,12 +2044,12 @@

                Simulation 4 - No Adjustment Effect

                # add in the actual data points points(x[1 : (n/2)], y[1 : (n/2)], pch = 21, col = "black", bg = "lightblue", cex = 1) points(x[(n/2 + 1) : n], y[(n/2 + 1) : n], pch = 21, col = "black", bg = "salmon", cex = 1) -

                +

                # print treatment and adjustment effects
                 rbind("Treatment Effect" = lm(y~t+x)$coef[2], "Adjustment Effect" = lm(y~t)$coef[2])
                -
                ##                            t
                -## Treatment Effect   0.9509680
                -## Adjustment Effect -0.1722953
                +
                ##                             t
                +## Treatment Effect   0.97140868
                +## Adjustment Effect -0.07835995
                • in the above graph, the elements are as follows:
                    @@ -2085,7 +2103,7 @@

                    Simulation 5 - Binary Interaction

                    # add in the actual data points points(x[1 : (n/2)], y[1 : (n/2)], pch = 21, col = "black", bg = "lightblue", cex = 1) points(x[(n/2 + 1) : n], y[(n/2 + 1) : n], pch = 21, col = "black", bg = "salmon", cex = 1) -

                    +

                    • in the above graph, the elements are as follows:
                        @@ -2122,7 +2140,7 @@

                        Simulation 6 - Continuous Adjustment

                        y <- beta0 + x1 * beta1 + beta2 * x2 + rnorm(n, sd = sigma) # plot y vs x1 and x2 qplot(x1, y) + geom_point(aes(colour=x2)) + geom_smooth(method = lm) -

                        +

                        • in the above graph, we plotted \(y\) vs \(x_{1}\) with \(x_{2}\) denoted as the gradient of color from blue to white
                        • to investigate the bivariate relationship more clearly, we can use the following command from the rgl package to generate 3D plots
                        • @@ -2148,13 +2166,13 @@

                          Simulation 6 - Continuous Adjustment

                          col = "black", bg = "lightblue", pch = 21, cex = 1) # add linear fit line abline(lm(I(resid(lm(y ~ x2))) ~ I(resid(lm(x1 ~ x2)))), lwd = 2) -

                          +

                          • from the generated plots above, we can see that
                            • \(x_{1}\) is unrelated to \(x_{2}\)
                            • \(x_{2}\) strongly correlated to \(y\)
                            • -
                            • relationship between \(x_{1}\) and \(y\) (loosely correlated \(\rightarrow\) \(R^2\) = 0.13) largely unchanged by when \(x_{2}\) is considered +
                            • relationship between \(x_{1}\) and \(y\) (loosely correlated \(\rightarrow\) \(R^2\) = 0.08) largely unchanged by when \(x_{2}\) is considered
                              • \(x_{2}\) captures the vast majority of variation in data
                              • there exists almost no residual variability after removing \(x_{2}\)
                              • @@ -2173,6 +2191,7 @@

                                Summary and Considerations

                              • it is often recommended to explore with simulations to see how inclusion or exclusion of another variable affects the relationship of variable of interest and the outcome
                              • variable selection simply affects associations between outcome and predictors, using the model to formulate causal relationship are even more difficult (entire field dedicated to this \(\rightarrow\) causal inference)
                              +

                              \(\pagebreak\)

    @@ -2220,7 +2239,7 @@

    Outliers and Influential Points

    points(0, 5, cex = 1.5, bg = "darkorange", col = "black", pch = 21) points(5, 5, cex = 1.5, bg = "darkorange", col = "black", pch = 21) points(5, 0, cex = 1.5, bg = "darkorange", col = "black", pch = 21) -

    +

    • different outliers can have varying degrees of influence
        @@ -2309,7 +2328,7 @@

        Example - Outlier Causing Linear Relationship

        fit <- lm(y ~ x) # add regression line to plot abline(fit) -

        +

        • data generated
            @@ -2319,7 +2338,7 @@

            Example - Outlier Causing Linear Relationship

          • there is no regression relationship between X and Y as the points are simply random noise
          • the regression relationship was able to be constructed precisely because of the presence of the point (10, 10)
              -
            • \(R^2\) = 0.2395867
            • +
            • \(R^2\) = 0.1886651
            • a single point has created a strong regression relationship where there shouldn’t be one
              • point (10, 10) has high leverage and high influence
              • @@ -2335,7 +2354,7 @@

                Example - Outlier Causing Linear Relationship

                # calculate the dfbetas for the slope the first 10 points
                 round(dfbetas(fit)[1 : 10, 2], 3)
                ##      1      2      3      4      5      6      7      8      9     10 
                -##  7.926  0.000 -0.060 -0.054 -0.006 -0.063 -0.009  0.057 -0.040 -0.004
                +## 7.279 -0.058 -0.032 -0.004 0.005 -0.044 0.005 0.074 -0.001 -0.023
                • as we can see from above, the slope coefficient would change dramatically if the first point (10, 10) is left out

                • hatvalues(fit) = measures the potential for influence for each point

                • @@ -2343,7 +2362,7 @@

                  Example - Outlier Causing Linear Relationship

                  # calculate the hat values for the first 10 points
                   round(hatvalues(fit)[1 : 10], 3)
                  ##     1     2     3     4     5     6     7     8     9    10 
                  -## 0.463 0.010 0.012 0.012 0.010 0.025 0.011 0.028 0.014 0.015
                  +## 0.486 0.011 0.015 0.010 0.015 0.022 0.010 0.014 0.015 0.015
                  • again, as we can see from above, the potential for influence is very large for the first point (10, 10)
                  @@ -2360,7 +2379,7 @@

                  Example - Real Linear Relationship

                  fit2 <- lm(y ~ x) # add the regression line to the plot abline(fit2) -

                  +

                  • data generated
                      @@ -2369,18 +2388,18 @@

                      Example - Real Linear Relationship

                  • there is a linear relationship between X and Y
                      -
                    • \(R^2\) = 0.9087977
                    • +
                    • \(R^2\) = 0.9421196
                    • point (5, 5) has high leverage and low influence
                  # calculate the dfbetas for the slope the first 10 points
                   round(dfbetas(fit2)[1 : 10, 2], 3)
                  ##      1      2      3      4      5      6      7      8      9     10 
                  -## -0.336  0.106 -0.001 -0.050 -0.086  0.052 -0.005 -0.015  0.013  0.019
                  +## 0.358 -0.061 0.012 0.028 0.029 0.026 -0.030 0.056 -0.069 0.025
                  # calculate the hat values for the first 10 points
                   round(hatvalues(fit2)[1 : 10], 3)
                  ##     1     2     3     4     5     6     7     8     9    10 
                  -## 0.248 0.018 0.010 0.012 0.012 0.013 0.011 0.010 0.010 0.013
                  +## 0.210 0.018 0.010 0.011 0.010 0.010 0.022 0.012 0.014 0.011
                  • as we can see from above, the point (5, 5) no longer has a large dfbetas value (indication of low influence) but still has a substantial hatvalue (indication of high leverage)
                      @@ -2417,6 +2436,7 @@

                      Example - Stefanski TAS 2007

                      # plot the residuals vs fitted values
                       plot(predict(fit), resid(fit), pch = '.')

                      +

                      \(\pagebreak\)

    @@ -2506,7 +2526,7 @@

    Example - \(R^2\) v \(n\) -

    +

    Adjusted \(R^2\)

    @@ -2559,9 +2579,9 @@

    Example - Unrelated Regressors

    "y ~ x1 + x2" = beta1.se[2], "y ~ x1 + x2 + x3" = beta1.se[3])
    ##                  beta1SE.x1
    -## y ~ x1              0.02906
    -## y ~ x1 + x2         0.02908
    -## y ~ x1 + x2 + x3    0.02909
    +## y ~ x1 0.03361 +## y ~ x1 + x2 0.03403 +## y ~ x1 + x2 + x3 0.03423
    • as we can see from the above result, if we include unrelated regressors \(x_2\) and \(x_3\), the standard error increases
    @@ -2604,9 +2624,9 @@

    Example - Highly Correlated Regressors / Variance Inflation

    "y ~ x1 + x2" = beta1.se[2], "y ~ x1 + x2 + x3" = beta1.se[3])
    ##                  beta1SE.x1
    -## y ~ x1              0.02935
    -## y ~ x1 + x2         0.04130
    -## y ~ x1 + x2 + x3    0.10668
    +## y ~ x1 0.02749 +## y ~ x1 + x2 0.03717 +## y ~ x1 + x2 + x3 0.10681
    • as we can see from above, adding highly correlated regressors drastically increases the standard errors of the coefficients

    • to estimate the actual change in variance, we can use the ratio of estimated variances for the \(\beta_1\) coefficient for the different models @@ -2624,16 +2644,16 @@

      Example - Highly Correlated Regressors / Variance Inflation

      # calculate the ratio of variances of beta1 for 2nd and 3rd models with respect to 1st model c(summary(lm(y ~ x1 + x2))$cov.unscaled[2,2], summary(lm(y~ x1 + x2 + x3))$cov.unscaled[2,2]) / a - 1 -
      ## [1]  1.087561 12.297903
      +
      ## [1]  0.9004773 12.6634243
      # alternatively, the change in variance can be estimated by calculating ratio of trials variance
       temp <- apply(betas, 1, var); temp[2 : 3] / temp[1] - 1
      -
      ##        x1        x1 
      -##  0.980612 12.212411
      +
      ##         x1         x1 
      +##  0.8292502 14.1018805
      • as we can see from the above results
        • adding \(x_2\) increases the variance by approximately 1 fold
        • -
        • adding \(x_2\) and \(x_3\) increases the variance by approximately 12 folds
        • +
        • adding \(x_2\) and \(x_3\) increases the variance by approximately 14 folds
      • the estimated values from the 1000 trials are different but close to the true increases, and they will approach the true values as the number of trials increases
      @@ -2673,7 +2693,7 @@

      Example: Variance Inflation Factors

    # load car library
    -library(car)
    +p_load("car")
     # run linear regression on Fertility vs all other predictors
     fit <- lm(Fertility ~ . , data = swiss)
     # calculate the variance inflation factors
    @@ -2847,7 +2867,7 @@ 

    Example: Step-wise Model Search

    ## (Intercept) cyl wt ## 39.686 -1.508 -3.191
      -
    • as we can see from above, the best model that captures most of the variability in the data is simply mpg ~ cyl + wt
    • +
    • as we can see from above, the best model that captures most of the variability in the data is simply mpg ~ cyl + wt \(\pagebreak\)
    @@ -2994,6 +3014,7 @@

    Solving for Normal and Quasi-Likelihood Normal Equations

  • predicted mean responses can be solved from \[\hat \mu = g^{*1}(\hat \eta)\]
  • coefficients are interpreted as the expected change in the link function of the expected response per unit change in \(X_k\) holding other regressors constant, or \[\beta_k = g(E[Y | X_k = x_k + 1, X_{\sim k} = x_{\sim k}]) - g(E[Y | X_k = x_k, X_{\sim k}=x_{\sim k}])\]
  • +

    \(\pagebreak\)

    @@ -3139,7 +3160,7 @@

    Example - Logistic Regression

    # set x values for the points to be plotted
     x <- seq(-10, 10, length = 1000)
    -# "library(manipulate)" is needed to use the manipulate function
    +# "p_load("manipulate")" is needed to use the manipulate function
     manipulate(
         # plot the logistic regression curve
         plot(x, exp(beta0 + beta1 * x) / (1 + exp(beta0 + beta1 * x)),
    @@ -3254,6 +3275,7 @@ 

    Further resources

  • Brian Caffo’s lecture notes on: Simpson’s Paradox, Retrospective Case-control Studies
  • Open Intro Chapter on Logistic Regression
  • +

    \(\pagebreak\)

    @@ -3444,7 +3466,7 @@

    Example - Robust Standard Errors with Poisson Regression

    # load sandwich package
    -library(sandwich)
    +p_load("sandwich")
     # compute
     confint.agnostic <- function (object, parm, level = 0.95, ...)
     {
    @@ -3530,6 +3552,7 @@ 

    Further Resources

  • the function zeroinfl fits zero inflated Poisson (ziP) models to such data
  • +

    \(\pagebreak\)

    @@ -3581,7 +3604,7 @@

    Example - Fitting Piecewise Linear Function

    plot(x, y, frame = FALSE, pch = 21, bg = "lightblue") # plot fitted values lines(x, yhat, col = "red", lwd = 2)
    -

    +

    Example - Fitting Piecewise Quadratic Function

    @@ -3599,7 +3622,7 @@

    Example - Fitting Piecewise Quadratic Function

    plot(x, y, frame = FALSE, pch = 21, bg = "lightblue") # plot fitted values lines(x, yhat, col = "red", lwd = 2)
    -

    +

    Example - Harmonics using Linear Models

    @@ -3632,7 +3655,7 @@

    Example - Harmonics using Linear Models

    for (i in 1 : 8) abline(v = i, lwd = 3, col = grey(.8)) # plot the linear regression fits lines(c(0, 1 : 8, 9), c(0, coef(fit)^2, 0), type = "l", lwd = 3, col = "red") -

    +

    • as we can see from above, the correct notes were identified
    • we can also use the Fast Fourier Transforms to identify the notes @@ -3645,7 +3668,7 @@

      Example - Harmonics using Linear Models

      a <- fft(chord) # plot only the real components of the fft plot(Re(a)^2, type = "l") -

      +

      • Note: the algorithm checks for all possible notes at all frequencies it can detect, which is why the peaks are very high in magnitude
      • Note: the symmetric display of the notes are due to periodic symmetries of the sine functions
      • diff --git a/7_REGMODS/Regression_Models_Course_Notes.pdf b/7_REGMODS/Regression_Models_Course_Notes.pdf index 3caf0d6..a0a3fca 100644 Binary files a/7_REGMODS/Regression_Models_Course_Notes.pdf and b/7_REGMODS/Regression_Models_Course_Notes.pdf differ diff --git a/8_PREDMACHLEARN/Practical Machine Learning Course Notes.Rmd b/8_PREDMACHLEARN/Practical Machine Learning Course Notes.Rmd index 96fae3d..dac86f0 100644 --- a/8_PREDMACHLEARN/Practical Machine Learning Course Notes.Rmd +++ b/8_PREDMACHLEARN/Practical Machine Learning Course Notes.Rmd @@ -2,13 +2,13 @@ title: "Practical Machine Learning Course Notes" author: "Xing Su" output: - pdf_document: - toc: yes - toc_depth: 3 html_document: highlight: pygments theme: spacelab toc: yes + pdf_document: + toc: yes + toc_depth: 3 header-includes: \usepackage{graphicx} \usepackage{mathtools} --- @@ -22,8 +22,11 @@ $\pagebreak$ - ***Note**: choosing the right dataset and knowing what the specific question is are paramount to the success of the prediction algorithm (GoogleFlu failed to predict accurately when people's search habits changed) * ```{r fig.width = 4, fig.height = 3, fig.align = 'center', echo = FALSE, message = FALSE} -library(png);library(grid) -library(doMC); registerDoMC(cores = 4) +# install grid and png packages if not present +if (!require("pacman")) install.packages("pacman") +p_load("png", "grid") +p_load("doMC") +registerDoMC(cores = 4) grid.raster(readPNG("figures/1.png")) ``` @@ -65,7 +68,8 @@ grid.raster(readPNG("figures/1.png")) ```{r message = FALSE, fig.width = 3, fig.height = 3, fig.align = 'center'} # load data -library(kernlab); data(spam); set.seed(333) +p_load("kernlab") +data(spam); set.seed(333) # picking a small subset (10 values) from spam data set smallSpam <- spam[sample(dim(spam)[1],size=10),] # label spam = 2 and ham = 1 @@ -352,7 +356,7 @@ grid.raster(readPNG("figures/13.png")) ```{r message = FALSE} # load packages and data -library(caret) +p_load("caret") # create training set indexes with 75% of data inTrain <- createDataPartition(y=spam$type,p=0.75, list=FALSE) # subset spam data to training @@ -487,7 +491,7 @@ args(trainControl) ```{r fig.width = 5, fig.height = 5, fig.align = 'center'} # load relevant libraries -library(ISLR); library(ggplot2); +p_load("ISLR", "ggplot2") # load wage data data(Wage) # create training and test sets @@ -508,15 +512,15 @@ qplot(age,wage,colour=education,data=training)+geom_smooth(method='lm',formula=y ``` * `cut2(variable, g=3)` = creates a new factor variable by cutting the specified variable into n groups (3 in this case) based on percentiles - - ***Note**: `cut2` function is part of the `Hmisc` package, so `library(Hmisc)` must be run first * + - ***Note**: `cut2` function is part of the `Hmisc` package, so `p_load("Hmisc")` must be run first * - this variable can then be used to tabulate/plot the data * `grid.arrange(p1, p2, ncol=2)` = `ggplot2` function the print multiple graphs on the same plot - - ***Note**: `grid.arrange` function is part of the `gridExtra` package, so `library(gridExtra)` must be run first * + - ***Note**: `grid.arrange` function is part of the `gridExtra` package, so `p_load("gridExtra")` must be run first * ```{r fig.width = 7, fig.height = 3, fig.align = 'center', message = FALSE} # load Hmisc and gridExtra packages -library(Hmisc);library(gridExtra); -# cute the wage variable +p_load("Hmisc", "gridExtra") +# cut the wage variable cutWage <- cut2(training$wage,g=3) # plot the boxplot p1 <- qplot(cutWage,age, data=training,fill=cutWage, @@ -595,6 +599,7 @@ rbind(train = c(mean = mean(trainCapAveS), std = sd(trainCapAveS)), ```{r message = FALSE, fig.width = 5, fig.height = 3, fig.align = 'center'} # set up BoxCox transforms +p_load("e1071") preObj <- preProcess(training[,-58],method=c("BoxCox")) # perform preprocessing on training data trainCapAveS <- predict(preObj,training[,-58])$capitalAve @@ -610,6 +615,7 @@ par(mfrow=c(1,2)); hist(trainCapAveS); qqnorm(trainCapAveS) ```{r message = FALSE} # Make some values NA +p_load("RANN") training$capAve <- training$capitalAve selectNA <- rbinom(dim(training)[1],size=1,prob=0.05)==1 training$capAve[selectNA] <- NA @@ -691,7 +697,7 @@ nearZeroVar(training,saveMetrics=TRUE) ```{r fig.width = 4, fig.height = 3, fig.align = 'center'} # load splines package -library(splines) +p_load("splines") # create polynomial function bsBasis <- bs(training$age,df=3) # fit the outcome on the three polynomial terms @@ -767,6 +773,7 @@ plot(prComp$x[,1],prComp$x[,2],col=typeColor,xlab="PC1",ylab="PC2") ```{r message = FALSE, warning = FALSE, fig.width = 4, fig.height = 3, fig.align = 'center'} # create train and test sets +p_load("caret") inTrain <- createDataPartition(y=spam$type,p=0.75, list=FALSE) training <- spam[inTrain,] testing <- spam[-inTrain,] @@ -775,7 +782,8 @@ preProc <- preProcess(log10(training[,-58]+1),method="pca",pcaComp=2) # calculate PCs for training data trainPC <- predict(preProc,log10(training[,-58]+1)) # run model on outcome and principle components -modelFit <- train(training$type ~ .,method="glm",data=trainPC) +modelFit <- train(training$type ~.,data=trainPC,method="glm") + # calculate PCs for test data testPC <- predict(preProc,log10(testing[,-58]+1)) # compare results @@ -1003,6 +1011,15 @@ modFit <- train(Species ~ .,method="rpart",data=training) # print the classification tree print(modFit$finalModel) # plot the classification tree +# Note: Need to install Gtk+ 2.x before using rattle. On OS/X: +# brew install gtk+ +# I should just need to run the following: +#p_load("rattle") +# But I actually need to run this, because apparently the dependencies aren't loading: +p_load("RGtk2") +p_load("rpart") +p_load("rpart.plot") +p_load("rattle") rattle::fancyRpartPlot(modFit$finalModel) # predict on test values predict(modFit,newdata=testing) @@ -1024,7 +1041,8 @@ predict(modFit,newdata=testing) ```{r fig.width = 5, fig.height = 4, fig.align = 'center', message = FALSE, warning = FALSE, cache = FALSE} # load data -library(ElemStatLearn); data(ozone,package="ElemStatLearn") +p_load("ElemStatLearn") +data(ozone,package="ElemStatLearn") # reorder rows based on ozone variable ozone <- ozone[order(ozone$ozone),] # create empty matrix @@ -1065,7 +1083,8 @@ lines(1:155,apply(ll,2,mean),col="red",lwd=2) ```{r fig.width = 5, fig.height = 4, fig.align = 'center', message = FALSE, warning = FALSE, cache = FALSE} # load relevant package and data -library(party); data(ozone,package="ElemStatLearn") +p_load("party") +data(ozone,package="ElemStatLearn") # reorder rows based on ozone variable ozone <- ozone[order(ozone$ozone),] # extract predictors @@ -1128,6 +1147,7 @@ grid.raster(readPNG("figures/14.png")) * ***example*** ```{r fig.width = 5, fig.height = 3, fig.align = 'center', message = FALSE, warning = FALSE, cache = FALSE} +p_load("randomForest") # load data data(iris) # create train/test data sets @@ -1208,6 +1228,7 @@ grid.raster(readPNG("figures/18.png")) * ***example*** ```{r message = FALSE, warning = FALSE, cache = FALSE} +p_load("gbm") # load data data(Wage) # remove log wage variable (we are trying to predict wage) @@ -1321,6 +1342,7 @@ pred.lda - ***example: `caret` package*** ```{r message = F, warning = F} +p_load("klaR") # using the same data from iris, run naive Bayes on training data nb <- train(Species ~ ., data=training,method="nb") # predict test outcomes using naive Bayes model @@ -1541,7 +1563,7 @@ lm(lpsa ~ .,data =small) ```{r echo = FALSE, fig.width = 5, fig.height = 5, fig.align = 'center', message = FALSE, warning = FALSE} # load MASS library for lm.ridge function -library(MASS) +p_load("MASS") # we will run the models for 10 different values from 0 to 50 lambdas <- seq(0,50,len=10) # create empty vectors for training and test residual sum squares @@ -1640,7 +1662,7 @@ legend("topright",covnames,pch=as.character(1:8), cex = 0.5) ```{r fig.width = 5, fig.height = 5, fig.align = 'center', message = F, warning = F, results = 'hide'} # load lars package -library(lars) +p_load("lars") # perform lasso regression lasso.fit <- lars(as.matrix(x), y, type="lasso", trace=TRUE) # plot lasso regression model @@ -1820,7 +1842,7 @@ $\pagebreak$ ```{r fig.width = 5, fig.height = 4.5, fig.align = 'center', message = FALSE, warning = FALSE, cache = FALSE} # load quantmod package -library(quantmod); +p_load("quantmod"); # specify to and from dates from.dat <- as.Date("01/01/00", format="%m/%d/%y") to.dat <- as.Date("3/2/15", format="%m/%d/%y") @@ -1838,7 +1860,7 @@ plot(decompose(ts),xlab="Years") ```{r fig.width = 5, fig.height = 3.5, fig.align = 'center', message = FALSE, warning = FALSE} # load forecast library -library(forecast) +p_load("forecast") # find the number of rows (years) rows <- ceiling(length(ts)/12) # use 90% of the data to create training set diff --git a/8_PREDMACHLEARN/Practical_Machine_Learning_Course_Notes.html b/8_PREDMACHLEARN/Practical_Machine_Learning_Course_Notes.html index 1c9b791..a7e7591 100644 --- a/8_PREDMACHLEARN/Practical_Machine_Learning_Course_Notes.html +++ b/8_PREDMACHLEARN/Practical_Machine_Learning_Course_Notes.html @@ -155,6 +155,7 @@

        Xing Su

    +

    \(\pagebreak\)

    Prediction

      @@ -219,7 +220,8 @@

      In Sample vs Out of Sample Errors

    • example
    # load data
    -library(kernlab); data(spam); set.seed(333)
    +p_load("kernlab")
    +data(spam); set.seed(333)
     # picking a small subset (10 values) from spam data set
     smallSpam <- spam[sample(dim(spam)[1],size=10),]
     # label spam = 2 and ham = 1
    @@ -594,7 +596,7 @@ 

    Data Slicing

    # load packages and data
    -library(caret)
    +p_load("caret")
     # create training set indexes with 75% of data
     inTrain <- createDataPartition(y=spam$type,p=0.75, list=FALSE)
     # subset spam data to training
    @@ -723,9 +725,9 @@ 

    Training Options (tuto
    # returns the arguments of the default train function
     args(train.default)
    ## function (x, y, method = "rf", preProcess = NULL, ..., weights = NULL, 
    -##     metric = ifelse(is.factor(y), "Accuracy", "RMSE"), maximize = ifelse(metric == 
    -##         "RMSE", FALSE, TRUE), trControl = trainControl(), tuneGrid = NULL, 
    -##     tuneLength = 3) 
    +##     metric = ifelse(is.factor(y), "Accuracy", "RMSE"), maximize = ifelse(metric %in% 
    +##         c("RMSE", "logLoss"), FALSE, TRUE), trControl = trainControl(), 
    +##     tuneGrid = NULL, tuneLength = 3) 
     ## NULL

    ## function (method = "boot", number = ifelse(grepl("cv", method), 
     ##     10, 25), repeats = ifelse(grepl("cv", method), 1, number), 
    -##     p = 0.75, initialWindow = NULL, horizon = 1, fixedWindow = TRUE, 
    -##     verboseIter = FALSE, returnData = TRUE, returnResamp = "final", 
    -##     savePredictions = FALSE, classProbs = FALSE, summaryFunction = defaultSummary, 
    -##     selectionFunction = "best", preProcOptions = list(thresh = 0.95, 
    -##         ICAcomp = 3, k = 5), index = NULL, indexOut = NULL, timingSamps = 0, 
    +##     p = 0.75, search = "grid", initialWindow = NULL, horizon = 1, 
    +##     fixedWindow = TRUE, verboseIter = FALSE, returnData = TRUE, 
    +##     returnResamp = "final", savePredictions = FALSE, classProbs = FALSE, 
    +##     summaryFunction = defaultSummary, selectionFunction = "best", 
    +##     preProcOptions = list(thresh = 0.95, ICAcomp = 3, k = 5), 
    +##     sampling = NULL, index = NULL, indexOut = NULL, timingSamps = 0, 
     ##     predictionBounds = rep(FALSE, 2), seeds = NA, adaptive = list(min = 5, 
     ##         alpha = 0.05, method = "gls", complete = TRUE), trim = FALSE, 
     ##     allowParallel = TRUE) 
    @@ -815,7 +818,7 @@ 

    Plotting Predictors (# load relevant libraries -library(ISLR); library(ggplot2); +p_load("ISLR", "ggplot2") # load wage data data(Wage) # create training and test sets @@ -838,17 +841,17 @@

    Plotting Predictors (# load Hmisc and gridExtra packages -library(Hmisc);library(gridExtra); -# cute the wage variable +p_load("Hmisc", "gridExtra") +# cut the wage variable cutWage <- cut2(training$wage,g=3) # plot the boxplot p1 <- qplot(cutWage,age, data=training,fill=cutWage, @@ -951,6 +954,7 @@

    Preprocessing (
    # set up BoxCox transforms
    +p_load("e1071")
     preObj <- preProcess(training[,-58],method=c("BoxCox"))
     # perform preprocessing on training data
     trainCapAveS <- predict(preObj,training[,-58])$capitalAve
    @@ -967,6 +971,7 @@ 

    Preprocessing (
    # Make some values NA
    +p_load("RANN")
     training$capAve <- training$capitalAve
     selectNA <- rbinom(dim(training)[1],size=1,prob=0.05)==1
     training$capAve[selectNA] <- NA
    @@ -979,7 +984,7 @@ 

    Preprocessing ( # compute differences between imputed values and true values quantile(capAve - capAveTruth)

    ##            0%           25%           50%           75%          100% 
    -## -1.656344e+00  2.377772e-05  1.286900e-03  1.881653e-03  3.174413e-01
    +## -1.656344e+00 2.377772e-05 1.286900e-03 1.880821e-03 3.174413e-01

    \(\pagebreak\)

    @@ -1087,7 +1092,7 @@

    Creating Splines (Polynomial Functions)

  • Note: the same polynomial operations must be performed on the test sets using the predict function
  • # load splines package
    -library(splines)
    +p_load("splines")
     # create polynomial function
     bsBasis <- bs(training$age,df=3)
     # fit the outcome on the three polynomial terms
    @@ -1202,6 +1207,7 @@ 

    caret Package

    # create train and test sets
    +p_load("caret")
     inTrain <- createDataPartition(y=spam$type,p=0.75, list=FALSE)
     training <- spam[inTrain,]
     testing <- spam[-inTrain,]
    @@ -1210,7 +1216,8 @@ 

    caret Package

    # calculate PCs for training data trainPC <- predict(preProc,log10(training[,-58]+1)) # run model on outcome and principle components -modelFit <- train(training$type ~ .,method="glm",data=trainPC) +modelFit <- train(training$type ~.,data=trainPC,method="glm") + # calculate PCs for test data testPC <- predict(preProc,log10(testing[,-58]+1)) # compare results @@ -1566,8 +1573,17 @@

    Constructing Trees with caret Package

    ## 6) Petal.Width< 1.65 34 1 versicolor (0.00000000 0.97058824 0.02941176) * ## 7) Petal.Width>=1.65 36 2 virginica (0.00000000 0.05555556 0.94444444) *
    # plot the classification tree
    +# Note: Need to install Gtk+ 2.x before using rattle. On OS/X:
    +# brew install gtk+
    +# I should just need to run the following:
    +#p_load("rattle")
    +# But I actually need to run this, because apparently the dependencies aren't loading:
    +p_load("RGtk2")
    +p_load("rpart")
    +p_load("rpart.plot")
    +p_load("rattle")
     rattle::fancyRpartPlot(modFit$finalModel)
    -

    +

    # predict on test values
     predict(modFit,newdata=testing)
    ##  [1] setosa     setosa     setosa     setosa     setosa     setosa    
    @@ -1601,7 +1617,8 @@ 

    Bagging

    # load data
    -library(ElemStatLearn); data(ozone,package="ElemStatLearn")
    +p_load("ElemStatLearn")
    +data(ozone,package="ElemStatLearn")
     # reorder rows based on ozone variable
     ozone <- ozone[order(ozone$ozone),]
     # create empty matrix
    @@ -1649,7 +1666,8 @@ 

    Bagging Algorithms

  • example
  • # load relevant package and data
    -library(party); data(ozone,package="ElemStatLearn")
    +p_load("party")
    +data(ozone,package="ElemStatLearn")
     # reorder rows based on ozone variable
     ozone <- ozone[order(ozone$ozone),]
     # extract predictors
    @@ -1725,7 +1743,8 @@ 

    R Commands and Examples

  • example
  • -
    # load data
    +
    p_load("randomForest")
    +# load data
     data(iris)
     # create train/test data sets
     inTrain <- createDataPartition(y=iris$Species,p=0.7, list=FALSE)
    @@ -1816,7 +1835,8 @@ 

    R Commands and Examples

  • predict function can be used to apply the model to test data, similar to the rest of the algorithms in caret package

  • example

  • -
    # load data
    +
    p_load("gbm")
    +# load data
     data(Wage)
     # remove log wage variable (we are trying to predict wage)
     Wage <- subset(Wage,select=-c(logwage))
    @@ -1834,21 +1854,19 @@ 

    R Commands and Examples

    ## ## No pre-processing ## Resampling: Bootstrapped (25 reps) -## ## Summary of sample sizes: 2102, 2102, 2102, 2102, 2102, 2102, ... -## ## Resampling results across tuning parameters: ## ## interaction.depth n.trees RMSE Rsquared RMSE SD Rsquared SD -## 1 50 35.64972 0.3317422 1.495165 0.02312183 -## 1 100 34.95593 0.3429594 1.503223 0.02319651 -## 1 150 34.84473 0.3451634 1.496016 0.02324176 -## 2 50 34.91119 0.3462101 1.490004 0.02425481 -## 2 100 34.74433 0.3487227 1.480423 0.02278780 -## 2 150 34.74823 0.3487136 1.472941 0.02314265 -## 3 50 34.83480 0.3467828 1.493846 0.02292988 -## 3 100 34.85342 0.3449018 1.482881 0.02373242 -## 3 150 34.99413 0.3401694 1.544378 0.02498133 +## 1 50 35.64593 0.3315218 1.511464 0.02408774 +## 1 100 34.97085 0.3423112 1.494712 0.02364722 +## 1 150 34.85844 0.3444257 1.475027 0.02325510 +## 2 50 34.92773 0.3455122 1.461015 0.02480965 +## 2 100 34.73593 0.3493371 1.455731 0.02362533 +## 2 150 34.76625 0.3479029 1.470360 0.02284358 +## 3 50 34.77908 0.3490059 1.503824 0.02475093 +## 3 100 34.80627 0.3466450 1.482543 0.02647141 +## 3 150 34.95168 0.3421691 1.500831 0.02650571 ## ## Tuning parameter 'shrinkage' was held constant at a value of 0.1 ## @@ -2000,7 +2018,8 @@

    Naive Bayes

  • example: caret package
  • -
    # using the same data from iris, run naive Bayes on training data
    +
    p_load("klaR")
    +# using the same data from iris, run naive Bayes on training data
     nb <- train(Species ~ ., data=training,method="nb")
     # predict test outcomes using naive Bayes model
     pred.nb <- predict(nb,testing)
    @@ -2367,7 +2386,7 @@ 

    Regularized Regression - LASSO Regression

  • example: lars package
  • # load lars package
    -library(lars)
    +p_load("lars")
     # perform lasso regression
     lasso.fit <- lars(as.matrix(x), y, type="lasso", trace=TRUE)
     # plot lasso regression model
    @@ -2608,7 +2627,7 @@ 

    R Commands and Examples

  • example: decomposed time series
  • # load quantmod package
    -library(quantmod);
    +p_load("quantmod");
     # specify to and from dates
     from.dat <- as.Date("01/01/00", format="%m/%d/%y")
     to.dat <- as.Date("3/2/15", format="%m/%d/%y")
    @@ -2621,12 +2640,12 @@ 

    R Commands and Examples

    ts <- ts(Cl(mAAPL), frequency = 12) # plot the decomposed parts of the time series plot(decompose(ts),xlab="Years")
    -

    +

    • example: forecast
    # load forecast library
    -library(forecast)
    +p_load("forecast")
     # find the number of rows (years)
     rows <- ceiling(length(ts)/12)
     # use 90% of the data to create training set
    @@ -2637,22 +2656,22 @@ 

    R Commands and Examples

    plot(ts.train) # add the moving average in red lines(ma(ts.train,order=3),col="red")
    -

    +

    # compute the exponential smoothing average
     ets <- ets(ts.train,model="MMM")
     # construct a forecasting model using the exponential smoothing function
     fcast <- forecast(ets)
     # plot forecast and add actual data in red
     plot(fcast); lines(ts.test,col="red")
    -

    +

    # print the accuracy of the forecast model
     accuracy(fcast,ts.test)
    -
    ##                      ME      RMSE       MAE       MPE     MAPE      MASE
    -## Training set  0.1188298  2.825883  1.646959  -0.61217 10.68901 0.1924329
    -## Test set     -7.8132889 16.736910 15.079222 -13.64900 20.31005 1.7618772
    -##                    ACF1 Theil's U
    -## Training set 0.09773823        NA
    -## Test set     0.84664431  3.360515
    +
    ##                        ME      RMSE       MAE         MPE     MAPE
    +## Training set   0.05089724  2.604205  1.427588  -0.8627284 10.32254
    +## Test set     -10.68215774 16.711650 14.965273 -16.8508577 20.70343
    +##                   MASE       ACF1 Theil's U
    +## Training set 0.1716568 0.06484368        NA
    +## Test set     1.7994616 0.83467782  3.480289

    \(\pagebreak\)

    diff --git a/8_PREDMACHLEARN/Practical_Machine_Learning_Course_Notes.pdf b/8_PREDMACHLEARN/Practical_Machine_Learning_Course_Notes.pdf index eee0c0c..a8ac49c 100644 Binary files a/8_PREDMACHLEARN/Practical_Machine_Learning_Course_Notes.pdf and b/8_PREDMACHLEARN/Practical_Machine_Learning_Course_Notes.pdf differ