Your SlideShare is downloading. ×
0
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

An Interactive Introduction To R (Programming Language For Statistics)

45,168

Published on

This is an interactive introduction to R. …

This is an interactive introduction to R.

R is an open source language for statistical computing, data analysis, and graphical visualization.

While most commonly used within academia, in fields such as computational biology and applied statistics, it is gaining currency in industry as well – both Facebook and Google use R within their firms.

3 Comments
161 Likes
Statistics
Notes
No Downloads
Views
Total Views
45,168
On Slideshare
0
From Embeds
0
Number of Embeds
30
Actions
Shares
0
Downloads
0
Comments
3
Likes
161
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • These two men can help you. They are Robert Gentleman and Ross Ihaka, the creators of R.R is:free, open sourcecreated by statisticians extensible via packages - over 1000 packagesR is an open source programming language for statistical computing, data analysis, and graphical visualization.It has one million users worldwide, and its user base is growing. While most commonly used within academia, in fields such as computational biology and applied statistics, it is gaining currency in commercial areas such as quantitative finance – it is used by Barclay’s – and business intelligence – both Facebook and Google use R within their firms.It was created by two men at the University of Auckland – pictured in the NYT article on the rightOther languages exist that can do some of what R does, but here’s what sets it apart:1. Created by StatisticiansBo Cowgill, who uses R at Google has said: “the great thing about R is that it was created by statisticians.” By this – I can’t speak for him – that R has unparalleled built-in support for statistics. But he also says “the terrible thing about R is… that it was created by statisticians.” The learning curve can be steep, and the documentation for functions is sometimes sparse. Free, open sourcethe importance of this can’t be understated. anyone can improve to the core language, and in fact, a group of few dozen developers around the world do exactly this. the language is constantly vetted, tweaked, and improved.Extensible via packagesthis is related to the open source nature of the language. R has a core set of functions it uses, but just as Excel has ‘add-ons’ and Matlab has ‘toolkits’, it is extensible with ‘packages’. This is where R is most powerful: there are over 1000 different packages that have been written for R. If there’s a new statistical technique or method that has been published, there’s a good chance it has been implemented in R.Audience survey: How many of you use R regularly? Have ever used R? Have ever heard of R?
  • These are the three fundamental steps of a data analyst.
  • Transcript

    • 1. An Interactive Introduction to R<br />November 2009<br />Michael E. Driscoll, Ph.D.<br />med@dataspora.com<br />http://www.dataspora.com<br />Daniel Murphy FCAS, MAAA<br />dmurphy@trinostics.com<br />
    • 2. January 6, 2009<br />
    • 3.
    • 4. R is a tool for…<br />Data Manipulation<br />connecting to data sources<br />slicing &amp; dicing data<br />Modeling &amp; Computation<br />statistical modeling<br />numerical simulation<br />Data Visualization<br />visualizing fit of models<br />composing statistical graphics<br />
    • 5. R is an environment<br />
    • 6. Its interface is plain<br />
    • 7. Let’s take a tour of some claim datain R<br />
    • 8. Let’s take a tour of some claim datain R<br />## load in some Insurance Claim data<br />library(MASS)<br />data(Insurance)<br />Insurance &amp;lt;- edit(Insurance)<br />head(Insurance)<br />dim(Insurance)<br />## plot it nicely using the ggplot2 package<br />library(ggplot2)<br />qplot(Group, Claims/Holders,<br /> data=Insurance,<br />geom=&amp;quot;bar&amp;quot;,<br /> stat=&amp;apos;identity&amp;apos;,<br /> position=&amp;quot;dodge&amp;quot;,<br /> facets=District ~ .,<br /> fill=Age,<br />ylab=&amp;quot;Claim Propensity&amp;quot;,<br />xlab=&amp;quot;Car Group&amp;quot;)<br />## hypothesize a relationship between Age ~ Claim Propensity<br />## visualize this hypothesis with a boxplot<br />x11()<br />library(ggplot2)<br />qplot(Age, Claims/Holders,<br /> data=Insurance,<br />geom=&amp;quot;boxplot&amp;quot;,<br /> fill=Age)<br />## quantify the hypothesis with linear model<br />m &amp;lt;- lm(Claims/Holders ~ Age + 0, data=Insurance)<br />summary(m)<br />
    • 9. R is “an overgrown calculator”<br />sum(rgamma(rpois(1,lambda=2),shape=49,scale=.2)))<br />
    • 10. R is “an overgrown calculator”<br /><ul><li>simple math</li></ul>&amp;gt; 2+2<br />4<br /><ul><li>storing results in variables</li></ul>&amp;gt; x &amp;lt;- 2+2 ## ‘&amp;lt;-’ is R syntax for ‘=’ or assignment<br />&amp;gt; x^2 <br />16<br /><ul><li>vectorized math</li></ul>&amp;gt; weight &amp;lt;- c(110, 180, 240) ## three weights<br />&amp;gt; height &amp;lt;- c(5.5, 6.1, 6.2) ## three heights<br />&amp;gt; bmi &amp;lt;- (weight*4.88)/height^2 ## divides element-wise<br />17.7 23.6 30.4<br />
    • 11. R is “an overgrown calculator”<br /><ul><li>basic statistics</li></ul>mean(weight) sd(weight) sqrt(var(weight))<br />176.6 65.0 65.0 # same as sd<br /><ul><li>set functions</li></ul>union intersect setdiff<br /><ul><li>advanced statistics</li></ul> &amp;gt; pbinom(40, 100, 0.5) ## P that a coin tossed 100 times<br /> 0.028## that comes up 40 heads is ‘fair’<br /> &amp;gt; pshare &amp;lt;- pbirthday(23, 365, coincident=2)<br /> 0.530 ## probability that among 23 people, two share a birthday<br />
    • 12. Try It! #1 Overgrown Calculator<br /><ul><li>basic calculations</li></ul>&amp;gt; 2 + 2 [Hit ENTER]<br />&amp;gt; log(100) [Hit ENTER]<br /><ul><li>calculate the value of $100 after 10 years at 5%</li></ul>&amp;gt; 100 * exp(0.05*10) [Hit ENTER]<br /><ul><li>construct a vector & do a vectorized calculation</li></ul>&amp;gt; year &amp;lt;- (1,2,5,10,25) [Hit ENTER] this returns an error. why?<br />&amp;gt; year &amp;lt;- c(1,2,5,10,25) [Hit ENTER]<br />&amp;gt; 100 * exp(0.05*year) [Hit ENTER]<br />
    • 13. R is a numerical simulator <br /><ul><li>built-in functions for classical probability distributions
    • 14. let’s simulate 10,000 trials of 100 coin flips. what’s the distribution of heads?</li></ul>&amp;gt; heads &amp;lt;- rbinom(10^5,100,0.50)<br />&amp;gt; hist(heads)<br />
    • 15. Functions for Probability Distributions<br />&amp;gt; pnorm(0) 0.05 <br />&amp;gt; qnorm(0.9) 1.28<br />&amp;gt; rnorm(100) vector of length 100<br />
    • 16. Functions for Probability Distributions<br />How to find the functions for lognormal distribution? <br />1) Use the double question mark ‘??’ to search<br />&amp;gt; ??lognormal<br />2) Then identify the package<br /> &amp;gt; ?Lognormal<br />3) Discover the dist functions dlnorm, plnorm, qlnorm, rlnorm<br />
    • 17. Try It! #2 Numerical Simulation<br /><ul><li>simulate 1m policy holders from which we expect 4 claims</li></ul>&amp;gt; numclaims &amp;lt;- rpois(n, lambda)<br />(hint: use ?rpoisto understand the parameters)<br /><ul><li>verify the mean & variance are reasonable</li></ul>&amp;gt; mean(numclaims)<br />&amp;gt; var(numclaims)<br /><ul><li>visualize the distribution of claim counts</li></ul>&amp;gt; hist(numclaims)<br />
    • 18. Getting Data In<br /> - from Files<br />&amp;gt; Insurance &amp;lt;- read.csv(“Insurance.csv”,header=TRUE)<br /> from Databases<br />&amp;gt; con &amp;lt;- dbConnect(driver,user,password,host,dbname)<br />&amp;gt; Insurance &amp;lt;- dbSendQuery(con, “SELECT * FROM claims”)<br /> from the Web<br />&amp;gt; con &amp;lt;- url(&amp;apos;http://labs.dataspora.com/test.txt&amp;apos;)<br />&amp;gt; Insurance &amp;lt;- read.csv(con, header=TRUE)<br /> from R objects<br />&amp;gt; load(‘Insurance.RData’)<br />
    • 19. Getting Data Out<br />to Files<br />write.csv(Insurance,file=“Insurance.csv”)<br />to Databases<br />con &amp;lt;- dbConnect(dbdriver,user,password,host,dbname)<br />dbWriteTable(con, “Insurance”, Insurance)<br /> to R Objects<br />save(Insurance, file=“Insurance.RData”)<br />
    • 20. Navigating within the R environment<br />listing all variables<br />&amp;gt; ls()<br />examining a variable ‘x’<br />&amp;gt; str(x)<br />&amp;gt; head(x)<br />&amp;gt; tail(x)<br />&amp;gt; class(x)<br />removing variables<br />&amp;gt; rm(x)<br />&amp;gt; rm(list=ls()) # remove everything<br />
    • 21. Try It! #3 Data Processing <br /><ul><li>load data & view it</li></ul>library(MASS)<br />head(Insurance) ## the first 7 rows<br />dim(Insurance) ## number of rows &amp; columns<br /><ul><li>write it out</li></ul>write.csv(Insurance,file=“Insurance.csv”, row.names=FALSE)<br />getwd() ## where am I?<br /><ul><li>view it in Excel, make a change, save it</li></ul> remove the first district<br /><ul><li>load it back in to R & plot it</li></ul>Insurance &amp;lt;- read.csv(file=“Insurance.csv”)<br />plot(Claims/Holders ~ Age, data=Insurance)<br />
    • 22. A Swiss-Army Knife for Data<br />
    • 23. A Swiss-Army Knife for Data<br />Indexing<br />Three ways to index into a data frame<br />array of integer indices<br />array of character names<br />array of logical Booleans<br />Examples:<br />df[1:3,]<br />df[c(“New York”, “Chicago”),]<br />df[c(TRUE,FALSE,TRUE,TRUE),]<br />df[city == “New York”,]<br />
    • 24. A Swiss-Army Knife for Data<br /><ul><li>subset – extract subsets meeting some criteria</li></ul>subset(Insurance, District==1)<br />subset(Insurance, Claims &amp;lt; 20)<br /><ul><li>transform – add or alter a column of a data frame</li></ul>transform(Insurance, Propensity=Claims/Holders)<br />cut – cut a continuous value into groups<br /> cut(Insurance$Claims, breaks=c(-1,100,Inf), labels=c(&amp;apos;lo&amp;apos;,&amp;apos;hi&amp;apos;)) <br />Put it all together: create a new, transformed data frame<br />transform(subset(Insurance, District==1), <br />ClaimLevel=cut(Claims, breaks=c(-1,100,Inf), <br /> labels=c(‘lo’,’hi’)))<br />
    • 25. A Statistical Modeler<br />R’s has a powerful modeling syntax<br />Models are specified with formulae, like <br /> y ~ x<br /> growth ~ sun + water<br />model relationships between continuous and categorical variables.<br />Models are also guide the visualization of relationships in a graphical form<br />
    • 26. A Statistical Modeler<br /><ul><li>Linear model</li></ul>m &amp;lt;- lm(Claims/Holders ~ Age, data=Insurance)<br /><ul><li>Examine it</li></ul> summary(m)<br /><ul><li>Plot it</li></ul> plot(m)<br />
    • 27. A Statistical Modeler<br />Logistic model<br /> m &amp;lt;- glm(Age ~ Claims/Holders, data=Insurance,<br /> family=binomial(“logit”))<br />)<br />Examine it<br /> summary(m)<br />Plot it<br /> plot(m)<br />
    • 28. Try It! #4 Statistical Modeling<br /><ul><li>fit a linear model</li></ul>m &amp;lt;- lm(Claims/Holders ~ Age + 0, data=Insurance) <br /><ul><li>examine it </li></ul>summary(m)<br /><ul><li>plot it</li></ul>plot(m)<br />
    • 29. Visualization: Multivariate Barplot<br />library(ggplot2)<br />qplot(Group, Claims/Holders,<br /> data=Insurance,<br /> geom=&amp;quot;bar&amp;quot;,<br /> stat=&amp;apos;identity&amp;apos;,<br /> position=&amp;quot;dodge&amp;quot;,<br /> facets=District ~ ., <br /> fill=Age)<br />
    • 30. Visualization: Boxplots<br />library(ggplot2)<br />qplot(Age, Claims/Holders, <br /> data=Insurance,<br /> geom=&amp;quot;boxplot“)<br />library(lattice)<br />bwplot(Claims/Holders ~ Age, <br /> data=Insurance)<br />
    • 31. Visualization: Histograms<br />library(ggplot2)<br />qplot(Claims/Holders, <br /> data=Insurance, <br /> facets=Age ~ ., geom=&amp;quot;density&amp;quot;)<br />library(lattice)<br />densityplot(~ Claims/Holders | Age, data=Insurance, layout=c(4,1)<br />
    • 32. Try It! #5 Data Visualization<br /><ul><li>simple line chart</li></ul>&amp;gt; x &amp;lt;- 1:10<br />&amp;gt; y &amp;lt;- x^2<br />&amp;gt; plot(y ~ x)<br /><ul><li>box plot</li></ul>&amp;gt; library(lattice)<br />&amp;gt; boxplot(Claims/Holders ~ Age, data=Insurance)<br /><ul><li>visualize a linear fit</li></ul>&amp;gt; abline()<br />
    • 33. Getting Help with R<br />Help within R itself for a function<br />&amp;gt; help(func)<br />&amp;gt; ?func<br />For a topic<br />&amp;gt; help.search(topic)<br />&amp;gt; ??topic<br />search.r-project.org<br />Google Code Search www.google.com/codesearch<br />Stack Overflow http://stackoverflow.com/tags/R<br />R-help list http://www.r-project.org/posting-guide.html<br />
    • 34. Six Indispensable Books on R<br />Learning R<br />Data Manipulation<br />Visualization<br />Statistical Modeling<br />
    • 35. Extending R with Packages<br />Over one thousand user-contributed packages are available on CRAN – the Comprehensive R Archive Network<br />http://cran.r-project.org<br />Install a package from the command-line<br />&amp;gt; install.packages(‘actuar’)<br />Install a package from the GUI menu<br />“Packages”--&amp;gt; “Install packages(s)”<br />
    • 36. Final Try It!Simulate a Tweedie<br /><ul><li>Simulate the number of claims from a Poisson distribution with λ=2 (NB: mean poisson = λ, variance poisson = λ)
    • 37. For as many claims as were randomly simulated, simulate a severity from a gamma distribution with shape α=49 and scale θ=0.2 (NB: mean gamma = αθ, variance gamma = αθ2)
    • 38. Is the total simulated claim amount close to expected?
    • 39. Calculate usual parameterization (μ,p,φ)of this Tweedie distribution
    • 40. Extra credit:
    • 41. Repeat the above 10000 times.
    • 42. Does your histogram look like Glenn Meyers’?http://www.casact.org/newsletter/index.cfm?fa=viewart&id=5756</li></li></ul><li>Final Try It!Simulate a Tweedie- ANSWERS<br />Simulate the number of claims from a Poisson distribution with λ=2 (NB: mean poisson = λ, variance poisson = λ)<br />rpois(1,lambda=2)<br />For as many claims as were randomly simulated, simulate a severity from a gamma distribution with shape α=49 and scale θ=0.2<br />rgamma(rpois(1,lambda=2),shape=49,scale=.2)<br />Is the total simulated claim amount close to expected?<br /> sum(rgamma(rpois(1,lambda=2),shape=49,scale=.2))<br /> Repeat the above 10000 times<br /> replicate(10000,<br /> sum(rgamma(rpois(1,lambda=2),shape=49,scale=.2)))<br /> Visualize the distribution<br />hist(replicate(10000,<br /> sum(rgamma(rpois(1,lambda=2),shape=49,scale=.2))),<br /> breaks=200, freq=FALSE)<br />
    • 43. P&amp;C Actuarial Models<br />Design • Construction Collaboration • Education Valuable • Transparent Daniel Murphy, FCAS, MAAAdmurphy@trinostics.com<br />925.381.9869<br />From Data to Decision<br />Big Data • Analytics • Visualization<br />www.dataspora.com<br />Michael E. Driscoll, Ph.D.<br />med@dataspora.com<br />415.860.4347<br />37<br />Contact Us<br />
    • 44. Appendices<br />R as a Programming Language<br />Advanced Visualization<br />Embedding R in a Server Environment<br />
    • 45. R as a Programming Language<br />fibonacci &amp;lt;- function(n) {<br /> fib &amp;lt;- numeric(n)<br /> fib [1:2] &amp;lt;- 1<br /> for (i in 3:n) { <br /> fib[i] &amp;lt;- fib[i-1] + fib[i-2]<br /> }<br /> return(fib[n])<br />}<br />Image from cover of Abelson &amp; Sussman’stextThe Structure and Interpretation of Computer Languages<br />
    • 46. Assignment<br />x &amp;lt;- c(1,2,6)<br />x a variable x<br />&amp;lt;- R’s assignment operator, equivalent to ‘=‘ <br />c( a function c which combines its arguments into a vector<br />y &amp;lt;- c(‘apples’,’oranges’)<br />z &amp;lt;- c(TRUE,FALSE)<br /> c(TRUE,FALSE) -&amp;gt; z<br />These are also valid assignment statements.<br />
    • 47. Function Calls<br /><ul><li>There are ~ 1100 built-in commands in the R “base” package, which can be executed on the command-line. The basic structure of a call is thus:</li></ul>output &amp;lt;- function(arg1, arg2, …)<br /><ul><li>Arithmetic Operations</li></ul> + - * / ^<br /><ul><li>R functions are typically vectorized</li></ul> x &amp;lt;- x/3<br /> works whether x is a one or many-valued vector<br />
    • 48. Data Structures in R<br />numeric<br />x &amp;lt;- c(0,2:4)<br />vectors<br />Character<br />y &amp;lt;- c(“alpha”, “b”, “c3”, “4”)<br />logical<br />z &amp;lt;- c(1, 0, TRUE, FALSE)<br />&amp;gt; class(x)<br />[1] &amp;quot;numeric&amp;quot;<br />&amp;gt; x2 &amp;lt;- as.logical(x)<br />&amp;gt; class(x2)<br />[1] “logical”<br />
    • 49. Data Structures in R<br />lists<br />lst &amp;lt;- list(x,y,z)<br />objects<br />matrices<br />M &amp;lt;- matrix(rep(x,3),ncol=3)<br />data frames*<br />df &amp;lt;- data.frame(x,y,z)<br />&amp;gt; class(df)<br />[1] “data.frame&amp;quot;<br />
    • 50. Summary of Data Structures<br />?<br />matrices<br />vectors<br />data frames*<br />lists<br />
    • 51. Advanced Visualization<br />lattice, ggplot2, and colorspace<br />
    • 52. ggplot2 =grammar of graphics<br />
    • 53. ggplot2 =grammar ofgraphics<br />
    • 54. qplot(log(carat), log(price), data = diamonds, alpha=I(1/20)) + facet_grid(. ~ color)<br />Achieving small multiples with “facets”<br />
    • 55. lattice = trellis<br />(source: http://lmdvr.r-forge.r-project.org )<br />
    • 56. list of latticefunctions<br />densityplot(~ speed | type, data=pitch)<br />
    • 57. visualizing six dimensions<br />of MLB pitches with lattice<br />
    • 58. xyplot(x ~ y | type, data=pitch,<br />fill.color = pitch$color,<br />panel = function(x,y, fill.color, …, subscripts) {<br /> fill &amp;lt;- fill.color[subscripts]<br />panel.xyplot(x, y, fill= fill, …) })<br />
    • 59. Beautiful Colors with Colorspace<br />library(“Colorspace”)<br />red &amp;lt;- LAB(50,64,64)<br />blue &amp;lt;- LAB(50,-48,-48)<br />mixcolor(10, red, blue)<br />
    • 60. efficient plotting with hexbinplot<br />hexbinplot(log(price)~log(carat),data=diamonds,xbins=40)<br />
    • 61. Embedding R in a Web Server<br />Using Packages &amp; R in a Server Environment<br />
    • 62.
    • 63.
    • 64. LinuxApacheMySQLR<br />http://labs.dataspora.com/gameday<br />
    • 65. vs<br />Coding<br />Clicking<br />

    ×