SlideShare a Scribd company logo
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")
R version 2.15.2 (2012-10-26) -- "Trick or Treat"
Copyright (C) 2012 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: 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 locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
>
R version 2.15.2 (2012-10-26) -- "Trick or Treat"
Copyright (C) 2012 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: 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 locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> print("Hello World")
[1] "Hello World"
>
Character vector of length 1 (mode
and type (typeof) comes with it)
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> print("Hello World")
[1] "Hello World"
> print
function (x, ...)
UseMethod("print")
<bytecode: 0x0000000010a7a2c8>
<environment: namespace:base>
>
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> print("Hello World")
[1] "Hello World"
> print
function (x, ...)
UseMethod("print")
<bytecode: 0x0000000010a7a2c8>
<environment: namespace:base>
>
This means that code
Is compiled, not interpreted
-- thus faster, but not for today…
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> print("Hello World")
[1] "Hello World"
> print
function (x, ...)
UseMethod("print")
<bytecode: 0x0000000010a7a2c8>
<environment: namespace:base>
>
Environment where it is
defined. Not for now…
> print("Hello World")
[1] "Hello World"
> print
function (x, ...)
UseMethod("print")
<bytecode: 0x0000000010a7a2c8>
<environment: namespace:base>
> formals("print")
$x
$...
> ?print
starting 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 can
be easily added for new classes.
> print("Hello World")
[1] "Hello World"
> print
function (x, ...)
UseMethod("print")
<bytecode: 0x0000000010a7a2c8>
<environment: namespace:base>
> formals("print")
$x
$...
> ?print
starting 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 can
be easily added for new classes.
Generic function.
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.
> set.seed(1234)
> x <- runif(10)
> y <- 2 + 5 * x + rnorm(10)
> plot(x, y)
>
Random numbers are important.
r unif != run if, uniform
normal
Another generic function.
> set.seed(1234)
> x <- runif(10)
> y <- 2 + 5 * x + rnorm(10)
> plot(x, y)
> (n <- cor.test(x, y))
Pearson's product-moment correlation
data: x and y
t = 5.9327, df = 8, p-value = 0.0003487
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
0.6325328 0.9770136
sample estimates:
cor
0.9026646
>
Parenthesis: print in short.
> (n <- cor.test(x, y))
Pearson's product-moment correlation
data: x and y
t = 5.9327, df = 8, p-value = 0.0003487
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
0.6325328 0.9770136
sample estimates:
cor
0.9026646
> class(n)
[1] "htest"
>
> 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 "Pearson's 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 important
Class is an attribute.
..- attr(*, "conf.level")= num 0.95
- attr(*, "class")= chr "htest"
> methods(class="htest")
[1] print.htest*
Non-visible functions are asterisked
>
..- attr(*, "conf.level")= num 0.95
- attr(*, "class")= chr "htest"
> methods(class="htest")
[1] print.htest*
Non-visible functions are asterisked
> print.htest
Error: object 'print.htest' not found
>
..- attr(*, "conf.level")= num 0.95
- attr(*, "class")= chr "htest"
> methods(class="htest")
[1] print.htest*
Non-visible functions are asterisked
> print.htest
Error: object 'print.htest' not found
> getAnywhere("print.htest")
A single object matching ‘print.htest’ was found
It was found in the following places
registered S3 method for print from namespace stats
namespace:stats
with value
function (x, digits = 4, quote = TRUE, prefix = "", ...)
{
cat("n")
Gimme that damn thing!
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")
}
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.
> m <- lm(y ~ x)
>
lm linear regression
y ~ x formula, let’s get back to
this later
> m <- lm(y ~ x)
> m
Call:
lm(formula = y ~ x)
Coefficients:
(Intercept) x
1.740 4.857
>
lm linear regression
y ~ x formula, let’s get back to
this later
This makes life so much easier,
talk about it next year.
> m <- lm(y ~ x)
> m
Call:
lm(formula = y ~ x)
Coefficients:
(Intercept) x
1.740 4.857
> names(m)
[1] "coefficients" "residuals" "effects" "rank"
[5] "fitted.values" "assign" "qr" "df.residual"
[9] "xlevels" "call" "terms" "model"
>
lm linear regression
y ~ x formula, let’s get back to
this later
This makes life so much easier,
talk about it next year.
> print.lm
function (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>
>
> (s <- summary(m))
Call:
lm(formula = y ~ x)
Residuals:
Min 1Q Median 3Q Max
-0.7372 -0.4189 -0.2076 0.2832 1.2928
Coefficients:
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 ‘ ’ 1
Residual standard error: 0.6751 on 8 degrees of freedom
Multiple R-squared: 0.8148, Adjusted R-squared: 0.7917
F-statistic: 35.2 on 1 and 8 DF, p-value: 0.0003487
>
> coef(m)
(Intercept) x
1.740131 4.857122
>
> coef(m)
(Intercept) x
1.740131 4.857122
> coef(s)
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.740131 0.4538773 3.833924 0.0049900329
x 4.857122 0.8186989 5.932733 0.0003486669
>
> coef(m)
(Intercept) x
1.740131 4.857122
> coef(s)
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.740131 0.4538773 3.833924 0.0049900329
x 4.857122 0.8186989 5.932733 0.0003486669
> class(s)
[1] "summary.lm"
>
> coef(m)
(Intercept) x
1.740131 4.857122
> coef(s)
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.740131 0.4538773 3.833924 0.0049900329
x 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 found
It was found in the following places
registered S3 method for print from namespace stats
namespace:stats
with value
function (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$residuals
df <- x$df
rdf <- df[2L]
cat(if (!is.null(x$weights) && diff(range(x$weights)))
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*
> if (questions) {
+ answer(questions)
+ } else q("no")
# ERUG meeting -- December 11, 2012
# Poetry with R -- Dissecting the code
# P. Solymos

More Related Content

PPTX
Basics of Python programming (part 2)
ODP
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
PDF
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
PDF
Creating Domain Specific Languages in Python
PPTX
Introduction to Python and TensorFlow
PDF
Declarative Thinking, Declarative Practice
PPTX
Learn python in 20 minutes
PDF
Python Cheat Sheet
Basics of Python programming (part 2)
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Creating Domain Specific Languages in Python
Introduction to Python and TensorFlow
Declarative Thinking, Declarative Practice
Learn python in 20 minutes
Python Cheat Sheet

What's hot (20)

PDF
Declare Your Language: Type Checking
PDF
Declare Your Language: Transformation by Strategic Term Rewriting
PDF
Good Code
PDF
Declare Your Language: Name Resolution
PPT
You Can Do It! Start Using Perl to Handle Your Voyager Needs
ODP
Python quickstart for programmers: Python Kung Fu
PDF
Compiler Construction | Lecture 8 | Type Constraints
PDF
Declarative Type System Specification with Statix
PDF
CS4200 2019 | Lecture 3 | Parsing
PPTX
Introduction to the basics of Python programming (part 3)
PPTX
Graph Database Query Languages
PDF
CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
PDF
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
PDF
Matlab and Python: Basic Operations
PPT
Python tutorial
PDF
Advanced Python, Part 2
PPTX
Learn python - for beginners - part-2
DOCX
Lisp and prolog in artificial intelligence
PPT
Computation Chapter 4
PPT
Mixing functional and object oriented approaches to programming in C#
Declare Your Language: Type Checking
Declare Your Language: Transformation by Strategic Term Rewriting
Good Code
Declare Your Language: Name Resolution
You Can Do It! Start Using Perl to Handle Your Voyager Needs
Python quickstart for programmers: Python Kung Fu
Compiler Construction | Lecture 8 | Type Constraints
Declarative Type System Specification with Statix
CS4200 2019 | Lecture 3 | Parsing
Introduction to the basics of Python programming (part 3)
Graph Database Query Languages
CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
Matlab and Python: Basic Operations
Python tutorial
Advanced Python, Part 2
Learn python - for beginners - part-2
Lisp and prolog in artificial intelligence
Computation Chapter 4
Mixing functional and object oriented approaches to programming in C#
Ad

Viewers also liked (14)

PPTX
BURN-Solymos-Adat-klonozas-2014-07-16
PDF
NACCB 2016 Madison WI
PPTX
Viral - Viral Marketing - Buzz Marketing
PDF
Isec july2 h1_solymos
PDF
Editoria di Print On Demand: studio di usabilità dell'interfaccia del sito we...
PDF
Complex models in ecology: challenges and solutions
PPT
EASY WRITING MAGIC STEPS
PDF
How to deal with messy data?
PPTX
23 nisan
PPT
Editoria di Print on Demand: studio di usabilità del sito web aziendale e str...
PDF
Parallel Computing with R
PPT
Kesebangunan
PPTX
Social Cooking Experimente
PDF
小さく始めて後で困らないためのVPCとChefを使ったAWS運用
BURN-Solymos-Adat-klonozas-2014-07-16
NACCB 2016 Madison WI
Viral - Viral Marketing - Buzz Marketing
Isec july2 h1_solymos
Editoria di Print On Demand: studio di usabilità dell'interfaccia del sito we...
Complex models in ecology: challenges and solutions
EASY WRITING MAGIC STEPS
How to deal with messy data?
23 nisan
Editoria di Print on Demand: studio di usabilità del sito web aziendale e str...
Parallel Computing with R
Kesebangunan
Social Cooking Experimente
小さく始めて後で困らないためのVPCとChefを使ったAWS運用
Ad

Similar to Poetry with R -- Dissecting the code (20)

PPTX
R language introduction
PDF
[1062BPY12001] Data analysis with R / week 2
PDF
Data Analysis with R (combined slides)
PPT
Basics of R
PPTX
Unit I - 1R introduction to R program.pptx
PDF
Basics of R programming for analytics [Autosaved] (1).pdf
PDF
20170509 rand db_lesugent
PPT
R workshop
PDF
@ R reference
PDF
R command cheatsheet.pdf
PDF
01_introduction_lab.pdf
PDF
Reference card for R
PDF
Short Reference Card for R users.
PPT
Inroduction to r
PDF
R basics
PDF
R Programming Reference Card
PPTX
2015-10-23_wim_davis_r_slides.pptx on consumer
PDF
Introduction to R programming
DOCX
R Language
PPTX
Getting Started with R
R language introduction
[1062BPY12001] Data analysis with R / week 2
Data Analysis with R (combined slides)
Basics of R
Unit I - 1R introduction to R program.pptx
Basics of R programming for analytics [Autosaved] (1).pdf
20170509 rand db_lesugent
R workshop
@ R reference
R command cheatsheet.pdf
01_introduction_lab.pdf
Reference card for R
Short Reference Card for R users.
Inroduction to r
R basics
R Programming Reference Card
2015-10-23_wim_davis_r_slides.pptx on consumer
Introduction to R programming
R Language
Getting Started with R

Recently uploaded (20)

PPTX
Chapter 5: Probability Theory and Statistics
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PDF
2021 HotChips TSMC Packaging Technologies for Chiplets and 3D_0819 publish_pu...
PPT
Module 1.ppt Iot fundamentals and Architecture
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PPTX
Modernising the Digital Integration Hub
PDF
WOOl fibre morphology and structure.pdf for textiles
PDF
Architecture types and enterprise applications.pdf
PDF
1 - Historical Antecedents, Social Consideration.pdf
PPTX
TLE Review Electricity (Electricity).pptx
PDF
STKI Israel Market Study 2025 version august
PDF
Enhancing emotion recognition model for a student engagement use case through...
PDF
project resource management chapter-09.pdf
PDF
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
PPTX
OMC Textile Division Presentation 2021.pptx
PDF
Web App vs Mobile App What Should You Build First.pdf
PPTX
observCloud-Native Containerability and monitoring.pptx
PPT
What is a Computer? Input Devices /output devices
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Hybrid model detection and classification of lung cancer
Chapter 5: Probability Theory and Statistics
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
2021 HotChips TSMC Packaging Technologies for Chiplets and 3D_0819 publish_pu...
Module 1.ppt Iot fundamentals and Architecture
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
Modernising the Digital Integration Hub
WOOl fibre morphology and structure.pdf for textiles
Architecture types and enterprise applications.pdf
1 - Historical Antecedents, Social Consideration.pdf
TLE Review Electricity (Electricity).pptx
STKI Israel Market Study 2025 version august
Enhancing emotion recognition model for a student engagement use case through...
project resource management chapter-09.pdf
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
OMC Textile Division Presentation 2021.pptx
Web App vs Mobile App What Should You Build First.pdf
observCloud-Native Containerability and monitoring.pptx
What is a Computer? Input Devices /output devices
Group 1 Presentation -Planning and Decision Making .pptx
Hybrid model detection and classification of lung cancer

Poetry with R -- Dissecting the code

  • 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 Computing ISBN 3-900051-07-0 Platform: 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 locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.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 Computing ISBN 3-900051-07-0 Platform: 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 locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > print("Hello World") [1] "Hello World" > Character vector of length 1 (mode and type (typeof) comes with it)
  • 4. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > print("Hello World") [1] "Hello World" > print function (x, ...) UseMethod("print") <bytecode: 0x0000000010a7a2c8> <environment: namespace:base> >
  • 5. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > print("Hello World") [1] "Hello World" > print function (x, ...) UseMethod("print") <bytecode: 0x0000000010a7a2c8> <environment: namespace:base> > This means that code Is 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 locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > print("Hello World") [1] "Hello World" > print function (x, ...) UseMethod("print") <bytecode: 0x0000000010a7a2c8> <environment: namespace:base> > Environment where it is defined. Not for now…
  • 7. > print("Hello World") [1] "Hello World" > print function (x, ...) UseMethod("print") <bytecode: 0x0000000010a7a2c8> <environment: namespace:base> > formals("print") $x $... > ?print starting 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 can be easily added for new classes.
  • 8. > print("Hello World") [1] "Hello World" > print function (x, ...) UseMethod("print") <bytecode: 0x0000000010a7a2c8> <environment: namespace:base> > formals("print") $x $... > ?print starting 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 can be 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, uniform normal Another generic function.
  • 11. > set.seed(1234) > x <- runif(10) > y <- 2 + 5 * x + rnorm(10) > plot(x, y) > (n <- cor.test(x, y)) Pearson's product-moment correlation data: x and y t = 5.9327, df = 8, p-value = 0.0003487 alternative hypothesis: true correlation is not equal to 0 95 percent confidence interval: 0.6325328 0.9770136 sample estimates: cor 0.9026646 > Parenthesis: print in short.
  • 12. > (n <- cor.test(x, y)) Pearson's product-moment correlation data: x and y t = 5.9327, df = 8, p-value = 0.0003487 alternative hypothesis: true correlation is not equal to 0 95 percent confidence interval: 0.6325328 0.9770136 sample estimates: cor 0.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 "Pearson's 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 important Class 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.htest Error: 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.htest Error: object 'print.htest' not found > getAnywhere("print.htest") A single object matching ‘print.htest’ was found It was found in the following places registered S3 method for print from namespace stats namespace:stats with value function (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 regression y ~ x formula, let’s get back to this later
  • 20. > m <- lm(y ~ x) > m Call: lm(formula = y ~ x) Coefficients: (Intercept) x 1.740 4.857 > lm linear regression y ~ x formula, let’s get back to this later This makes life so much easier, talk about it next year.
  • 21. > m <- lm(y ~ x) > m Call: lm(formula = y ~ x) Coefficients: (Intercept) x 1.740 4.857 > names(m) [1] "coefficients" "residuals" "effects" "rank" [5] "fitted.values" "assign" "qr" "df.residual" [9] "xlevels" "call" "terms" "model" > lm linear regression y ~ x formula, let’s get back to this later This makes life so much easier, talk about it next year.
  • 22. > print.lm function (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.2928 Coefficients: 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 ‘ ’ 1 Residual standard error: 0.6751 on 8 degrees of freedom Multiple R-squared: 0.8148, Adjusted R-squared: 0.7917 F-statistic: 35.2 on 1 and 8 DF, p-value: 0.0003487 >
  • 25. > coef(m) (Intercept) x 1.740131 4.857122 > coef(s) Estimate Std. Error t value Pr(>|t|) (Intercept) 1.740131 0.4538773 3.833924 0.0049900329 x 4.857122 0.8186989 5.932733 0.0003486669 >
  • 26. > coef(m) (Intercept) x 1.740131 4.857122 > coef(s) Estimate Std. Error t value Pr(>|t|) (Intercept) 1.740131 0.4538773 3.833924 0.0049900329 x 4.857122 0.8186989 5.932733 0.0003486669 > class(s) [1] "summary.lm" >
  • 27. > coef(m) (Intercept) x 1.740131 4.857122 > coef(s) Estimate Std. Error t value Pr(>|t|) (Intercept) 1.740131 0.4538773 3.833924 0.0049900329 x 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 found It was found in the following places registered S3 method for print from namespace stats namespace:stats with value function (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$residuals df <- x$df rdf <- 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