Introduzione ad R

Alla fine di questa puntata…

  • dovreste avere un’idea della storia, della “filosofia”, dei pro e contro di R
  • dovreste avere un’idea delle risorse disponibili e di dove reperirle
  • dovreste avere una “certa” familiarità con l’ambiente di lavoro R-Studio
  • dovreste avere imparato a chiedere aiuto

e molto probabilmente…

  • potreste chiedervi “ma fa veramente al caso mio?”

Prima di cominciare… le presentazioni

Di cosa si tratta?

  • un ambiente integrato per l’analisi dei dati (numerica, tabellare e grafica)
  • un linguaggio a tutti gli effetti
  • inspirato al linguaggio S (sviluppato da John Chambers presso gli AT&T Laboratories, con il contributo di Douglas Bates, Rick Becker, Bill Cleveland, Trevor Hastie, Daryl Pregibon and Allan Wilks)
  • il progetto parte nel 1997 su iniziativa di Robert Gentleman e Ross Ihaka (“R & R”) del Statistics Department dell’Università di Auckland (Nuova Zelanda)

Pro e contro

Punti di forza:

  • Open source (GNU GpL)
  • Offre una varietà di tecniche statistiche sia di base che avanzate
  • Grafica avanzata e personalizzabile
  • Estensibile
  • Veloce prototipazione
  • Multipiattaforma (ambiente, dati e script)
  • Ricchezza delle librerie aggiuntive
  • Possibilità di utilizzare codice C, Fortran, ecc.
  • Gestione dati completamente in memoria

Punti di debolezza:

  • Curva di apprendimento ripida
  • Lentezza (non compilato)
  • Ambiente non user–friendly
  • Gestione dell’output complessa
  • Gestione dati completamente in memoria

Caratteristiche

Linguaggio:

  • di tipo espressione
  • funzionale
  • orientato agli oggetti

Modalità d’uso:

  • interattiva
  • programmazione

Da non dimenticare:

  • case–sensitive
  • \(>\) prompt principale
  • \(+\) prompt di continuazione istruzione

Quanta (troppa?) roba

Sys.Date()
[1] "2025-02-21"
nrow(available.packages())

18897

Ma ci sono ormai molte altre fonti (GitHub e dintorni):

install.packages("devtools")
?devtools::install_github

O anche:

install.packages("remotes")
?remotes::install_github

Un’alternativa recente per installare pacchetti da fonti multiple:

(sembrerebbe più agevole, ma ancora da esplorare per scaricare package da diverse fonti)

Come installare R

nrow(getCRANmirrors())
[1] 94
table(getCRANmirrors()$Country)

             0-Cloud    0-Cloud-East-Asia            Argentina 
                   1                    1                    1 
           Australia              Austria              Belgium 
                   3                    1                    2 
              Brazil             Bulgaria               Canada 
                   3                    1                    3 
               Chile                China             Colombia 
                   1                   13                    1 
          Costa Rica               Cyprus       Czech Republic 
                   1                    1                    1 
             Denmark              Ecuador              Finland 
                   1                    1                    1 
              France              Germany               Greece 
                   3                    8                    1 
             Hungary              Iceland                India 
                   1                    1                    2 
           Indonesia                 Iran                Italy 
                   1                    1                    2 
               Japan                Korea               Mexico 
                   1                    1                    2 
             Morocco          Netherlands          New Zealand 
                   1                    2                    1 
              Norway               Poland             Portugal 
                   1                    1                    1 
              Russia         South Africa                Spain 
                   1                    1                    2 
              Sweden          Switzerland               Taiwan 
                   1                    2                    1 
              Turkey                   UK United Arab Emirates 
                   1                    3                    1 
             Uruguay                  USA            Worldwide 
                   1                   11                    1 
  • http://cran.stat.unipd.it/

Documentazione e dintorni

Documentazione ufficiale:

Task Views:

The R Journal:

diverse mailing list generali e Special Interest Group (SIG):

Alcune risorse interessanti:

Cosa si dice in giro?

Benvenuti…

… si fa per dire:

Benvenuti?

Ambienti (?semplificati?) di utilizzo

Ambienti multipiattaforma:

Va un po’ meglio?

Benvenuti?

Ricerca nell’help

Due possibili modalità per effettuare la ricerca nell’help per parola chiave:

help.search("regression")
??regression

Per nome della funzione (anche qui due possibili modalità):

help(mean)
?mean

Per l’help di alcune funzioni (quelle di programmazione e gli operatori) è necessario usare gli apici (singoli o doppi):

?for
?"for"

E’ possibile cercare tutti gli oggetti (sulla “search list”) che contengono una data parola:

apropos("mean")
 [1] ".colMeans"     ".rowMeans"     "colMeans"      "kmeans"       
 [5] "mean"          "mean.Date"     "mean.default"  "mean.difftime"
 [9] "mean.POSIXct"  "mean.POSIXlt"  "rowMeans"      "weighted.mean"

così come cercare in che posizione si trovano:

find("mean")
[1] "package:base"
find("kmeans")
[1] "package:stats"

Le funzioni: istruzioni per l’uso

Gli esempi della funzioni possono essere provati usando la funzione example:

example(mean)

mean> x <- c(0:10, 50)

mean> xm <- mean(x)

mean> c(xm, mean(x, trim = 0.10))
[1] 8.75 5.50
example(plot)

plot> Speed <- cars$speed

plot> Distance <- cars$dist

plot> plot(Speed, Distance, panel.first = grid(8, 8),
plot+      pch = 0, cex = 1.2, col = "blue")


plot> plot(Speed, Distance,
plot+      panel.first = lines(stats::lowess(Speed, Distance), lty = "dashed"),
plot+      pch = 0, cex = 1.2, col = "blue")


plot> ## Show the different plot types
plot> x <- 0:12

plot> y <- sin(pi/5 * x)

plot> op <- par(mfrow = c(3,3), mar = .1+ c(2,2,3,1))

plot> for (tp in c("p","l","b",  "c","o","h",  "s","S","n")) {
plot+    plot(y ~ x, type = tp, main = paste0("plot(*, type = \"", tp, "\")"))
plot+    if(tp == "S") {
plot+       lines(x, y, type = "s", col = "red", lty = 2)
plot+       mtext("lines(*, type = \"s\", ...)", col = "red", cex = 0.8)
plot+    }
plot+ }


plot> par(op)

plot> ##--- Log-Log Plot  with  custom axes
plot> lx <- seq(1, 5, length.out = 41)

plot> yl <- expression(e^{-frac(1,2) * {log[10](x)}^2})

plot> y <- exp(-.5*lx^2)

plot> op <- par(mfrow = c(2,1), mar = par("mar")-c(1,0,2,0), mgp = c(2, .7, 0))

plot> plot(10^lx, y, log = "xy", type = "l", col = "purple",
plot+      main = "Log-Log plot", ylab = yl, xlab = "x")

plot> plot(10^lx, y, log = "xy", type = "o", pch = ".", col = "forestgreen",
plot+      main = "Log-Log plot with custom axes", ylab = yl, xlab = "x",
plot+      axes = FALSE, frame.plot = TRUE)


plot> my.at <- 10^(1:5)

plot> axis(1, at = my.at, labels = formatC(my.at, format = "fg"))

plot> e.y <- -5:-1 ; at.y <- 10^e.y

plot> axis(2, at = at.y, col.axis = "red", las = 1,
plot+      labels = as.expression(lapply(e.y, function(E) bquote(10^.(E)))))

plot> par(op)
example(plotmath)

pltmth> require(graphics)

pltmth> x <- seq(-4, 4, length.out = 101)

pltmth> y <- cbind(sin(x), cos(x))

pltmth> matplot(x, y, type = "l", xaxt = "n",
pltmth+         main = expression(paste(plain(sin) * phi, "  and  ",
pltmth+                                 plain(cos) * phi)),
pltmth+         ylab = expression("sin" * phi, "cos" * phi), # only 1st is taken
pltmth+         xlab = expression(paste("Phase Angle ", phi)),
pltmth+         col.main = "blue")


pltmth> axis(1, at = c(-pi, -pi/2, 0, pi/2, pi),
pltmth+      labels = expression(-pi, -pi/2, 0, pi/2, pi))

pltmth> ## How to combine "math" and numeric variables :
pltmth> plot(1:10, type="n", xlab="", ylab="", main = "plot math & numbers")


pltmth> theta <- 1.23 ; mtext(bquote(hat(theta) == .(theta)), line= .25)

pltmth> for(i in 2:9)
pltmth+     text(i, i+1, substitute(list(xi, eta) == group("(",list(x,y),")"),
pltmth+                             list(x = i, y = i+1)))

pltmth> ## note that both of these use calls rather than expressions.
pltmth> ##
pltmth> text(1, 10,  "Derivatives:", adj = 0)

pltmth> text(1, 9.6, expression(
pltmth+  "             first: {f * minute}(x) " == {f * minute}(x)), adj = 0)

pltmth> text(1, 9.0, expression(
pltmth+  "     second: {f * second}(x) "        == {f * second}(x)), adj = 0)

pltmth> ## note the "{ .. }" trick to get "chained" equations:
pltmth> plot(1:10, 1:10, main = quote(1 <= {1 < 2}))