Skip to main content

Posts

Showing posts from November, 2016

Grammar of Graphics (ggplot2 - Pt2)

# Convert cyl to factor (don't need to change) mtcars$cyl <- as.factor(mtcars$cyl) # Example from base R (don't need to change) plot(mtcars$wt, mtcars$mpg, col = mtcars$cyl) abline(lm(mpg ~ wt, data = mtcars), lty = 2) lapply(mtcars$cyl, function(x) {   abline(lm(mpg ~ wt, mtcars, subset = (cyl == x)), col = x)   }) legend(x = 5, y = 33, legend = levels(mtcars$cyl),        col = 1:3, pch = 1, bty = "n") # Plot 1: add geom_point() to this command to create a scatter plot ggplot(mtcars, aes(x = wt, y = mpg, col = cyl)) +   geom_point() # Plot 2: include the lines of the linear models, per cyl ggplot(mtcars, aes(x = wt, y = mpg, col = cyl)) +   geom_point() +   geom_smooth(method = "lm", se = FALSE) # Plot 3: include a lm for the entire dataset in its whole ggplot(mtcars, aes(x = wt, y = mpg, col = cyl)) +   geom_point() +   geom_smooth(method = "lm", se = FALSE) +   geom_smooth(aes(group = 1), method = "lm", se = FALSE, linety...

Grammar of Graphics (ggplot2 - Pt1)

Based on Book by Leland Wilkinson Elements : Data : the dataset to be plotted. Aesthetics : The scale on which we map our data Geometrics : The visual elements used for our data. Facets : Plotting small multiples Statistics : Representation of our data to aid understanding. Coordinates : The space on which the data will be  plotted. Themes : All non data ink. These are building blocks. # A scatter plot has been made for you ggplot(mtcars, aes(x = wt, y = mpg)) +   geom_point() # Replace ___ with the correct vector ggplot(mtcars, aes(x = wt, y = mpg, col = disp)) +   geom_point() # Replace ___ with the correct vector ggplot(mtcars, aes(x = wt, y = mpg, size = disp)) +   geom_point() # Explore the diamonds data frame with str() str(diamonds) # Add geom_point() with + ggplot(diamonds, aes(x = carat, y = price))+geom_point() # Add geom_point() and geom_smooth() with + ggplot(diamonds, aes(x = carat, y = price))+geom_point()+geom_smooth() # The plot you cre...

Learn from credit risk modeling - p1

Credit Analysis # View the structure of loan_data str(loan_data) # Load the gmodels package library(gmodels) # Call CrossTable() on loan_status CrossTable(loan_data$loan_status) # Call CrossTable() on grade and loan_status #CrossTable(x=loan_data$grade,y=loan_data$loan_status,prop.r=TRUE, prop.c=FALSE,prop.t=FALSE,Prop.chisq=FALSE) CrossTable(loan_data$grade, loan_data$loan_status, prop.r = TRUE,            prop.c = FALSE, prop.t = FALSE, prop.chisq = FALSE) we only want row-wise proportions, so set prop.r to TRUE, but prop.c , prop.t and prop.chisq to FALSE (default values here are TRUE, and this would lead to inclusion of column proportions, table proportions and chi-square contributions for each cell. We do not need these here.)# Create histogram of loan_amnt: hist_1 hist_1<-hist(loan_data$loan_amnt) # Print locations of the breaks in hist_1 hist_1$breaks #Knowing the location of the breaks is important because if they are poorly chosen, the histogram...

R Markdown Notes - P2

R functions sometimes return messages, warnings, and even error messages. By default, R Markdown will include these messages in your report. You can use the message, warning and error options to prevent R Markdown from displaying these. If any of the options are set to FALSE R Markdown will not include the corresponding type of message in the output. ``{r warning = FALSE, error = FALSE} "four" + "five" ``` Three of the most popular chunk options are echo, eval and results. If echo = FALSE, R Markdown will not display the code in the final document (but it will still run the code and display its results unless told otherwise). If eval = FALSE, R Markdown will not run the code or include its results, (but it will still display the code unless told otherwise). If results = 'hide', R Markdown will not display the results of the code (but it will still run the code and display the code itself unless told otherwise). You can embed R code into the text of your docu...

R Markdown Notes - P1

Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>. R markdown -> Literate Programming + Dynamic Documents + Reproducible Research We can produce outputs in PDF,HTML, Word, Powerpoint formats from a Markdown Document. Sample YAML header that contains some metadata --- title: "Hello World" author: "Srini" date: "January 1, 2015" output: html_document --- R code chunks surrounded by ```{r} and ```; a syntax that comes with the knitr package ```{r} summary(mtcars) ``` You can also embed plots, for example: ```{r, echo=FALSE} plot(mtcars) ``` Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot. You can use Markdown to embed formatting instructions into your text. Italics -> *italics* Bold the text ->**bold** Code Style ->`code` Hyperlink -> [RStudio](www.rstudio...