Your SlideShare is downloading. ×
Poetry with R -- Dissecting the code
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

Poetry with R -- Dissecting the code

332
views

Published on

ERUG meeting -- December 11, 2012

ERUG meeting -- December 11, 2012

Published in: Technology, Education

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
332
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
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

Transcript

  • 1. introduction()# Poetry is considered a form of literary art in which# language is used for its aesthetic and evocative qualities. It# contains multiple interpretations and therefore resonates# differently in each reader.## Code is the language used to communicate with computers. It has its# own rules (syntax) and meaning (semantics). Like literature writers# or poets, coders also have their own style that include - strategies# for optimizing the code being read by a computer, and facilitating# its understanding through visual organization and comments for other# coders.## Code can speak literature, logic, maths. It contains different# layers of abstraction and it links them to the physical world of# processors and memory chips. All these resources can contribute in# expanding the boundaries of contemporary poetry by using code as a# new language. Code to speak about life or death, love or hate. Code# meant to be read, not run.url("http://code-poems.com")
  • 2. R version 2.15.2 (2012-10-26) -- "Trick or Treat"Copyright (C) 2012 The R Foundation for Statistical ComputingISBN 3-900051-07-0Platform: x86_64-w64-mingw32/x64 (64-bit)R is free software and comes with ABSOLUTELY NO WARRANTY.You are welcome to redistribute it under certain conditions.Type license() or licence() for distribution details.Natural language support but running in an English localeR is a collaborative project with many contributors.Type contributors() for more information andcitation() on how to cite R or R packages in publications.Type demo() for some demos, help() for on-line help, orhelp.start() for an HTML browser interface to help.Type q() to quit R.>
  • 3. R version 2.15.2 (2012-10-26) -- "Trick or Treat"Copyright (C) 2012 The R Foundation for Statistical ComputingISBN 3-900051-07-0Platform: x86_64-w64-mingw32/x64 (64-bit)R is free software and comes with ABSOLUTELY NO WARRANTY.You are welcome to redistribute it under certain conditions.Type license() or licence() for distribution details.Natural language support but running in an English localeR is a collaborative project with many contributors.Type contributors() for more information andcitation() on how to cite R or R packages in publications.Type demo() for some demos, help() for on-line help, orhelp.start() for an HTML browser interface to help.Type q() to quit R.> print("Hello World")[1] "Hello World">Character vector of length 1 (modeand type (typeof) comes with it)
  • 4. Type license() or licence() for distribution details.Natural language support but running in an English localeR is a collaborative project with many contributors.Type contributors() for more information andcitation() on how to cite R or R packages in publications.Type demo() for some demos, help() for on-line help, orhelp.start() for an HTML browser interface to help.Type q() to quit R.> print("Hello World")[1] "Hello World"> printfunction (x, ...)UseMethod("print")<bytecode: 0x0000000010a7a2c8><environment: namespace:base>>
  • 5. Type license() or licence() for distribution details.Natural language support but running in an English localeR is a collaborative project with many contributors.Type contributors() for more information andcitation() on how to cite R or R packages in publications.Type demo() for some demos, help() for on-line help, orhelp.start() for an HTML browser interface to help.Type q() to quit R.> print("Hello World")[1] "Hello World"> printfunction (x, ...)UseMethod("print")<bytecode: 0x0000000010a7a2c8><environment: namespace:base>>This means that codeIs compiled, not interpreted-- thus faster, but not for today…
  • 6. Type license() or licence() for distribution details.Natural language support but running in an English localeR is a collaborative project with many contributors.Type contributors() for more information andcitation() on how to cite R or R packages in publications.Type demo() for some demos, help() for on-line help, orhelp.start() for an HTML browser interface to help.Type q() to quit R.> print("Hello World")[1] "Hello World"> printfunction (x, ...)UseMethod("print")<bytecode: 0x0000000010a7a2c8><environment: namespace:base>>Environment where it isdefined. Not for now…
  • 7. > print("Hello World")[1] "Hello World"> printfunction (x, ...)UseMethod("print")<bytecode: 0x0000000010a7a2c8><environment: namespace:base>> formals("print")$x$...> ?printstarting httpd help server ... done>Arglist, help also helps.x is an input object.print prints its argument and returns it invisibly (via invisible(x)).It is a generic function which means that new printing methods canbe easily added for new classes.
  • 8. > print("Hello World")[1] "Hello World"> printfunction (x, ...)UseMethod("print")<bytecode: 0x0000000010a7a2c8><environment: namespace:base>> formals("print")$x$...> ?printstarting httpd help server ... done>print prints its argument and returns it invisibly (via invisible(x)).It is a generic function which means that new printing methods canbe easily added for new classes.Generic function.
  • 9. summary(so_far)# o R is interpreted language, but# bite compiling is possible (see# compiler package).# o In the background, everything is# about environments (which are# similar to lists), but luckily, this# is hidden from average user.# o Everything is an object -- OO.# o Objects come in classes.# o Methods can be defined for objects.
  • 10. > set.seed(1234)> x <- runif(10)> y <- 2 + 5 * x + rnorm(10)> plot(x, y)>Random numbers are important.r unif != run if, uniformnormalAnother generic function.
  • 11. > set.seed(1234)> x <- runif(10)> y <- 2 + 5 * x + rnorm(10)> plot(x, y)> (n <- cor.test(x, y))Pearsons product-moment correlationdata: x and yt = 5.9327, df = 8, p-value = 0.0003487alternative hypothesis: true correlation is not equal to 095 percent confidence interval:0.6325328 0.9770136sample estimates:cor0.9026646>Parenthesis: print in short.
  • 12. > (n <- cor.test(x, y))Pearsons product-moment correlationdata: x and yt = 5.9327, df = 8, p-value = 0.0003487alternative hypothesis: true correlation is not equal to 095 percent confidence interval:0.6325328 0.9770136sample estimates:cor0.9026646> class(n)[1] "htest">
  • 13. > class(n)[1] "htest"> str(n)List of 9$ statistic : Named num 5.93..- attr(*, "names")= chr "t"$ parameter : Named int 8..- attr(*, "names")= chr "df"$ p.value : num 0.000349$ estimate : Named num 0.903..- attr(*, "names")= chr "cor"$ null.value : Named num 0..- attr(*, "names")= chr "correlation"$ alternative: chr "two.sided"$ method : chr "Pearsons product-moment correlation"$ data.name : chr "x and y"$ conf.int : atomic [1:2] 0.633 0.977..- attr(*, "conf.level")= num 0.95- attr(*, "class")= chr "htest">It is a list, nothing more.Attributes are importantClass is an attribute.
  • 14. ..- attr(*, "conf.level")= num 0.95- attr(*, "class")= chr "htest"> methods(class="htest")[1] print.htest*Non-visible functions are asterisked>
  • 15. ..- attr(*, "conf.level")= num 0.95- attr(*, "class")= chr "htest"> methods(class="htest")[1] print.htest*Non-visible functions are asterisked> print.htestError: object print.htest not found>
  • 16. ..- attr(*, "conf.level")= num 0.95- attr(*, "class")= chr "htest"> methods(class="htest")[1] print.htest*Non-visible functions are asterisked> print.htestError: object print.htest not found> getAnywhere("print.htest")A single object matching ‘print.htest’ was foundIt was found in the following placesregistered S3 method for print from namespace statsnamespace:statswith valuefunction (x, digits = 4, quote = TRUE, prefix = "", ...){cat("n")Gimme that damn thing!
  • 17. cat(strwrap(x$method, prefix = "t"), sep = "n")cat("n")cat("data: ", x$data.name, "n")out <- character()if (!is.null(x$statistic))out <- c(out, paste(names(x$statistic), "=", format(round(x$statistic,4))))if (!is.null(x$parameter))out <- c(out, paste(names(x$parameter), "=", format(round(x$parameter,3))))if (!is.null(x$p.value)) {fp <- format.pval(x$p.value, digits = digits)out <- c(out, paste("p-value", if (substr(fp, 1L, 1L) =="<") fp else paste("=", fp)))}cat(strwrap(paste(out, collapse = ", ")), sep = "n")if (!is.null(x$alternative)) {cat("alternative hypothesis: ")if (!is.null(x$null.value)) {if (length(x$null.value) == 1L) {alt.char <- switch(x$alternative, two.sided = "not equal to",less = "less than", greater = "greater than")cat("true", names(x$null.value), "is", alt.char,x$null.value, "n")}else {cat(x$alternative, "nnull values:n")print(x$null.value, ...)}}else cat(x$alternative, "n")}
  • 18. if (!is.null(x$conf.int)) {cat(format(100 * attr(x$conf.int, "conf.level")), "percent confidence interval:n",format(c(x$conf.int[1L], x$conf.int[2L])), "n")}if (!is.null(x$estimate)) {cat("sample estimates:n")print(x$estimate, ...)}cat("n")invisible(x)}<bytecode: 0x0000000010f7a3e0><environment: namespace:stats>>Return the value invisibly.Defined in stats pkg.
  • 19. > m <- lm(y ~ x)>lm linear regressiony ~ x formula, let’s get back tothis later
  • 20. > m <- lm(y ~ x)> mCall:lm(formula = y ~ x)Coefficients:(Intercept) x1.740 4.857>lm linear regressiony ~ x formula, let’s get back tothis laterThis makes life so much easier,talk about it next year.
  • 21. > m <- lm(y ~ x)> mCall:lm(formula = y ~ x)Coefficients:(Intercept) x1.740 4.857> names(m)[1] "coefficients" "residuals" "effects" "rank"[5] "fitted.values" "assign" "qr" "df.residual"[9] "xlevels" "call" "terms" "model">lm linear regressiony ~ x formula, let’s get back tothis laterThis makes life so much easier,talk about it next year.
  • 22. > print.lmfunction (x, digits = max(3, getOption("digits") - 3), ...){cat("nCall:n", paste(deparse(x$call), sep = "n", collapse = "n"),"nn", sep = "")if (length(coef(x))) {cat("Coefficients:n")print.default(format(coef(x), digits = digits), print.gap = 2,quote = FALSE)}else cat("No coefficientsn")cat("n")invisible(x)}<bytecode: 0x0000000010542380><environment: namespace:stats>>
  • 23. > (s <- summary(m))Call:lm(formula = y ~ x)Residuals:Min 1Q Median 3Q Max-0.7372 -0.4189 -0.2076 0.2832 1.2928Coefficients:Estimate Std. Error t value Pr(>|t|)(Intercept) 1.7401 0.4539 3.834 0.004990 **x 4.8571 0.8187 5.933 0.000349 ***---Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1Residual standard error: 0.6751 on 8 degrees of freedomMultiple R-squared: 0.8148, Adjusted R-squared: 0.7917F-statistic: 35.2 on 1 and 8 DF, p-value: 0.0003487>
  • 24. > coef(m)(Intercept) x1.740131 4.857122>
  • 25. > coef(m)(Intercept) x1.740131 4.857122> coef(s)Estimate Std. Error t value Pr(>|t|)(Intercept) 1.740131 0.4538773 3.833924 0.0049900329x 4.857122 0.8186989 5.932733 0.0003486669>
  • 26. > coef(m)(Intercept) x1.740131 4.857122> coef(s)Estimate Std. Error t value Pr(>|t|)(Intercept) 1.740131 0.4538773 3.833924 0.0049900329x 4.857122 0.8186989 5.932733 0.0003486669> class(s)[1] "summary.lm">
  • 27. > coef(m)(Intercept) x1.740131 4.857122> coef(s)Estimate Std. Error t value Pr(>|t|)(Intercept) 1.740131 0.4538773 3.833924 0.0049900329x 4.857122 0.8186989 5.932733 0.0003486669> class(s)[1] "summary.lm"> getAnywhere("print.summary.lm")A single object matching ‘print.summary.lm’ was foundIt was found in the following placesregistered S3 method for print from namespace statsnamespace:statswith valuefunction (x, digits = max(3, getOption("digits") - 3), symbolic.cor =x$symbolic.cor,signif.stars = getOption("show.signif.stars"), ...){cat("nCall:n", paste(deparse(x$call), sep = "n", collapse = "n"),"nn", sep = "")resid <- x$residualsdf <- x$dfrdf <- df[2L]cat(if (!is.null(x$weights) && diff(range(x$weights)))
  • 28. print(summay(presentation))# o Print method is simplest.# o It conveys meaning to user.# o Results are usually structures in# different ways,# o need methods to access information:> methods(class="lm")[1] add1.lm* alias.lm* anova.lm[4] case.names.lm* confint.lm* cooks.distance.lm*[7] deviance.lm* dfbeta.lm* dfbetas.lm*[10] drop1.lm* dummy.coef.lm* effects.lm*[13] extractAIC.lm* family.lm* formula.lm*[16] hatvalues.lm influence.lm* kappa.lm[19] labels.lm* logLik.lm* model.frame.lm[22] model.matrix.lm nobs.lm* plot.lm[25] predict.lm print.lm proj.lm*[28] qr.lm* residuals.lm rstandard.lm[31] rstudent.lm simulate.lm* summary.lm[34] variable.names.lm* vcov.lm*
  • 29. > if (questions) {+ answer(questions)+ } else q("no")# ERUG meeting -- December 11, 2012# Poetry with R -- Dissecting the code# P. Solymos