# Five Fast Things You Can Do With R # PREPARATION ------------------------------------------------------------ # 1. Insure this file, glass.data, glass.names, winequality-white.cvs, # winequality.names and BBands.R are on the desktop # 2. Open firefox to http://www.r-project.org, second tab to home # 3. rm .RData on Desktop # 4. Open this file, winequality-white.cvs, glass.data and BBands.R in Text Editor # 5. cd Desktop # 6. start R (type R) # SUB-TOPICS -------------------------------------------------------------- # Expain Easy / Hard / Very Powerful # Explain Overloading functions ============================================================================= # WHAT IS R? ================================================================ http://www.r-project.org/ # look at the site # WHAT I AM GOING TO TALK ABOUT ============================================= 1. gathering and reviewing data 2. calculating statistics 3. visualizing data (and stats) 4. modeling 5. scripting, an example ============================================================================= # GATHER DATA (and Review) ================================================= # WINE and GLASS --------------------------------------------------------- # Gather Data ------------------------------------------------------------ wine <- read.csv("winequality-white.csv", sep=";", header=TRUE) glass <- read.csv("glass.data", header = FALSE) # Review Data ------------------------------------------------------------ # Review Data High Level ------------------------------------------------- str(wine) # all numbers, integers and rational numbers str(glass) # all numbers, integers and rational numbers # Review Data Secific Ranges --------------------------------------------- wine[1:10, ] # Review the first 10 observations glass[1:20, ] # Review the first 20 observations glass[1:20, -3] # Review the first 20 observations, less 3rd column glass[1:20, c(-3, -5)] # Review the first 20 observations, less 3rd and 5th columns # CALCULATE STATISTICS ================================================== # Individual Statistics ------------------------------------------------ mean(wine) # individual statistic, but with Error Message! mean(glass) # individual statistic, but with Error Message! colMeans(wine) # individual statistic, better method colMeans(glass) # individual statistic, better method sd(wine) # individual statistic, but with Error Message! sd(glass) # individual statistic, but with Error Message! sapply(wine, sd) # individual statistic, better method sapply(glass, sd) # individual statistic, better method max(wine) # individual statistic for entire dataframe max(wine[,1]) # individual statistic for one column / varriable min(glass) # individual statistic # Summarized Statistics ------------------------------------------------ summary(wine) # statistics summarized summary(glass) # statistics summarized # More Detailed Statistics --------------------------------------------- library("pastecs") # load library stat.desc(wine) # generate detailed statistics stat.desc(glass[, 1:5]) # easier to read # VISUALIZE DATA ========================================================= # Summarized Plots ----------------------------------------------------- pairs(wine) # SPLOM pairs(glass) # SPLOM pairs(wine[, 1:5]) # for better viewing SPLOM pairs(glass[, 1:5]) # for better viewing SPLOM # Individual Scatter Plots ---------------------------------------------- plot(glass$V2, glass$V8) plot(wine[, c(6, 7)]) # Histograms Plots -- ---------------------------------------------------- hist(wine$density) hist(glass$V2) # Box Plots --------------------------------------------------------------- boxplot(glass$V2) # one variable boxplot(glass) # all at once boxplot(glass[,c( -1, -6)]) # remove two columns # Full Demo --------------------------------------------------------------- demo(graphics) # built-in graphics demo demo(persp) # built-in perspective graphics demo library(lattice) # load pacakge demo(lattice) # MODELING =================================================================== # Simple Example------------------------------------------------------------- plot(wine[, c(7, 6)]) # plot one variable against another lmwine <- lm(wine[, c(6, 7)]) # make a linear model abline(lmwine, col="red") # plot that model # Fuller Example------------------------------------------------------------- plot(wine[, c(1,6,8,9)]) # plot four variables against one another # Fullest Example ----------------------------------------------------------- library(psych) # load a package wineSample <- wine[c(1,6,8,9)] # look at a sub-sample pairs.panels(wineSample, lm=TRUE) # SCRIPTING ================================================================= source("BBands.R") # load an executable file and run it