Self Assessment 1 Answer Sheet

These are the answers/explanations to the self assessment I posted last week.

  1. Create the script \(~\)

  2. # 2/19/2020, Abigail Payne, Self Assessment 1

    \(~\)

  3. # 2/19/2020, Abigail Payne, Self Assessment 1
    
    ############ Library Functions ####################
    
    ###################################################

    \(~\)

  4. To install the package, run this line in the console:

    install.packages("survival")

    \(~\)

  5. First, start by typing ?survival in the console.

    ?survival
    ## No documentation for 'survival' in specified packages and libraries:
    ## you could try '??survival'

    Unfortunately, R tells you it can’t find any documentation for that package, but suggests you try ??survival

    ??survival

    The result won’t show up here, but the ?? command essentially searches for anything in any R documentation matching the word that comes after it and displays it in the help tab of the plots window. So, ??survival found all documentation with the word survival in it.
    The fact that no documentation showed up unfortunately means that none exists. R is a free, open-source programming language, meaning nothing is proprietary and anyone (including you!) can create and contribute to R packages. This often creates inconsistencies in the level of documentation available across the different packages. Some people put a lot time into creating clear and easy to read documentation with well laid out instructions and examples. But, as you can see here, many people don’t. Luckily, there is still one more thing we can try.

\(~\)

  1. Use the library(help = "packagename") command as a “last ditch” effort.

    library(help = "survival")

    By running this command, you can see that a list of datasets included in this package appeared in the scripting window. Nice! \(~\)

  2. ?diabetic
    ## No documentation for 'diabetic' in specified packages and libraries:
    ## you could try '??diabetic'

    \(~\)

  3. There could be no documentation, but we also skipped a crucial step, loading the package. Load the survival package by adding the library() function to the import statements section of your script. Remember to highlight the line and hit “run” in the top right corner of the scripting window.

    # 2/19/2020, Abigail Payne, Self Assessment 1
    
    ############ Library Functions ####################
    library(survival)
    ###################################################

    \(~\)

  4. :)

\(~\)

  1. In general, anything you do as “set up” or “back-ground” that is not crucial for the script to run properly should be done in the console. So, your library() statements should always be in your scripts, that way when you run the script, the package will always load. Commands like install.packages() and anything using the help command ? should not be included in your script, as you do not want these lines of code to run every time the script is run.

\(~\)

  1. ?diabetic

    \(~\)

  2. View(diabetic)

    \(~\)

  3. According to the documentation from the help command, the risk column represents the risk factor of the patient.

\(~\)

  1. # 2/19/2020, Abigail Payne, Self Assessment 1
    
    ############ Library Functions ####################
    library(survival)
    ###################################################
    
    ### Laser Count 
    table(diabetic$laser)
    ## 
    ## xenon argon 
    ##   228   166

\(~\)

  1. # 2/19/2020, Abigail Payne, Self Assessment 1
    
    ############ Library Functions ####################
    library(survival)
    ###################################################
    
    ### Laser Count 
    table(diabetic$laser)
    ## 
    ## xenon argon 
    ##   228   166
    # Age Histogram
    hist(diabetic$age)

    # Risk factor histogram
    hist(diabetic$risk)

\(~\)

  1. # 2/19/2020, Abigail Payne, Self Assessment 1
    
    ############ Library Functions ####################
    library(survival)
    ###################################################
    
    ### Laser Count 
    table(diabetic$laser)
    ## 
    ## xenon argon 
    ##   228   166
    # Age Histogram
    hist(diabetic$age,
         main = "Ages of Study Participants",
         xlab = "Age (Years)")

    # Risk factor histogram
    hist(diabetic$risk,
         main = "Risk Factor of Study Participants",
         xlab = "Risk Factor")

\(~\)

  1. # 2/19/2020, Abigail Payne, Self Assessment 1
    
    ############ Library Functions ####################
    library(survival)
    ###################################################
    
    ### Laser Count 
    table(diabetic$laser)
    ## 
    ## xenon argon 
    ##   228   166
    # Age Histogram
    hist(diabetic$age, breaks = 25,
         main = "Ages of Study Participants",
         xlab = "Age (Years)")

    # Risk factor histogram
    hist(diabetic$risk,
         main = "Risk Factor of Study Participants",
         xlab = "Risk Factor")

\(~\)

  1. Later last week I found this color document online [http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf?utm_source=twitterfeed&utm_medium=twitter]. I recommend bookmarking it!

    # 2/19/2020, Abigail Payne, Self Assessment 1
    
    ############ Library Functions ####################
    library(survival)
    ###################################################
    
    ### Laser Count 
    table(diabetic$laser)
    ## 
    ## xenon argon 
    ##   228   166
    # Age Histogram
    hist(diabetic$age, breaks = 25,
         main = "Ages of Study Participants",
         xlab = "Age (Years)",
    
         col = "orchid",
         border = "orchid4")

    # Risk factor histogram
    hist(diabetic$risk,
         main = "Risk Factor of Study Participants",
         xlab = "Risk Factor",
    
         col = "paleturquoise3",
         border = "paleturquoise4")

\(~\)

  1. Before the code, I included a detailed description of how to use the wesanderson package.

    # Always start by loading the package 
    library(wesanderson)
    
    # then I get the names of the available packages using the names() function
    names(wes_palettes)
    ##  [1] "BottleRocket1"  "BottleRocket2"  "Rushmore1"      "Rushmore"      
    ##  [5] "Royal1"         "Royal2"         "Zissou1"        "Darjeeling1"   
    ##  [9] "Darjeeling2"    "Chevalier1"     "FantasticFox1"  "Moonrise1"     
    ## [13] "Moonrise2"      "Moonrise3"      "Cavalcanti1"    "GrandBudapest1"
    ## [17] "GrandBudapest2" "IsleofDogs1"    "IsleofDogs2"

    To select colors from a palette, you have to use a function called wes_palette (not to be confused with the wes_palettes function we just used).

    You select a single color using this syntax, where the name of the palette is the first argument and the number of colors you want is the second argument.

    wes_palette("Rushmore", 1)

    You can select 2 colors, or 3 colors, or 4. There are a limited number of colors in each palette, so if you request more than that number R will give you and error.

    wes_palette("Rushmore", 2)

    wes_palette("Rushmore", 3)

    wes_palette("Rushmore", 4)

    wes_palette("Rushmore", 5)

    wes_palette("Rushmore", 6)
    ## Error in wes_palette("Rushmore", 6): Number of requested colors greater than what palette can offer

    The wes_palette function essentially creates a vector of hex color codes of the requested legnth, but it always does it in the same order. You can access any of the codes by indexing into the vector as such:

    wes_palette("Rushmore", 5)[c(3:5)]
    ## [1] "#0B775E" "#35274A" "#F2300F"

    You can see that I asked for the 3rd through 5th element of the created vector, and thats what I got!

    Here is how to use the code in the self assessment example:

    # 2/19/2020, Abigail Payne, Self Assessment 1
    
    ############ Library Functions ####################
    library(survival)
    library(wesanderson)
    ###################################################
    
    ### Laser Count 
    table(diabetic$laser)
    ## 
    ## xenon argon 
    ##   228   166
    # Age Histogram
    hist(diabetic$age, breaks = 25,
         main = "Ages of Study Participants",
         xlab = "Age (Years)",
    
         # I used the 
         col = wes_palette("Zissou1", 2),
         border = wes_palette("Zissou1", 4)[c(3:4)])

    # Risk factor histogram
    hist(diabetic$risk,
         main = "Risk Factor of Study Participants",
         xlab = "Risk Factor",
    
         col = wes_palette("Zissou1", 4)[4],
         border = wes_palette("Zissou1", 4)[3])

\(~\)

  1. Lastly, here are some interesting plots you can make using the plot() function :). I didn’t change the axis labels, title, or colors. This is an example of using the plot() function to quickly view the data and see what plots might be significant.

        # 2/19/2020, Abigail Payne, Self Assessment 1
    
    ############ Library Functions ####################
    library(survival)
    ###################################################
    
    plot(diabetic$age, diabetic$time)

    plot(diabetic$age, diabetic$risk)