introduction to r
DESCRIPTION
Darrin Rogers (not any kind of R expert guy) Department of Psychology & Anthropology University of Texas – Pan American 4/14/2011. Introduction to R. Installing R. Do this while I’m rambling on about R… http://cran.revolution-computing.com/ (CRAN mirror in San Antonio) - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Introduction to R](https://reader036.vdocuments.us/reader036/viewer/2022062305/568164c2550346895dd6d689/html5/thumbnails/1.jpg)
INTRODUCTION TO R
Darrin Rogers (not any kind of R expert guy)Department of Psychology & AnthropologyUniversity of Texas – Pan American
4/14/2011
![Page 2: Introduction to R](https://reader036.vdocuments.us/reader036/viewer/2022062305/568164c2550346895dd6d689/html5/thumbnails/2.jpg)
Installing R
Do this while I’m rambling on about R… http://cran.revolution-computing.com/
(CRAN mirror in San Antonio) Installation file is in [OS] base
Install wizard; defaults are probably OK for now
If no admin rights, install to C:\R Choose a working directory
Actually any number of them…. Make a shortcut to Rgui.exe Change target directory in shortcut properties
![Page 3: Introduction to R](https://reader036.vdocuments.us/reader036/viewer/2022062305/568164c2550346895dd6d689/html5/thumbnails/3.jpg)
Where did R come from?
S development started in 1976 (commercial) John Chambers et al., Bell labs 4 major revisions through 1990s
Mid- 990s: R (Ross Ihaka & Robert Gentleman) 1997 – R Core Team – modify source code CRAN R-project.org
Users: 200,000 – 10,000,000+ … ? Hard to say.
![Page 4: Introduction to R](https://reader036.vdocuments.us/reader036/viewer/2022062305/568164c2550346895dd6d689/html5/thumbnails/4.jpg)
What is R? Core: interpreted computer language
Developed principally for stats Branching and looping Modular programming using functions
Package: Base Essential low-level functions
Other packages Lots and lots Several included in “basic install” About 3,000 available through CRAN
FREE & open-source
![Page 5: Introduction to R](https://reader036.vdocuments.us/reader036/viewer/2022062305/568164c2550346895dd6d689/html5/thumbnails/5.jpg)
Basic R Resources
Comprehensive R Archive Network (CRAN) cran.r-project.org Package and Base downloads
Google R-Help mailing list archives Google Websites made for R users Google
![Page 6: Introduction to R](https://reader036.vdocuments.us/reader036/viewer/2022062305/568164c2550346895dd6d689/html5/thumbnails/6.jpg)
Installing Packages
Psych Install.packages(“psych”)
Load package library(psych)
Get help ?psych
Google “R-package psych” Documentation (a bit arcane) Google easier-to-understand guides
![Page 7: Introduction to R](https://reader036.vdocuments.us/reader036/viewer/2022062305/568164c2550346895dd6d689/html5/thumbnails/7.jpg)
R basics Use a text editor
I like notepad++ (“R aware”) Notepad works OK
Case-sensitive! Psych ≠ psych
Help ?something ??something RSiteSearch(“something”) google
![Page 8: Introduction to R](https://reader036.vdocuments.us/reader036/viewer/2022062305/568164c2550346895dd6d689/html5/thumbnails/8.jpg)
Objects
Many kinds of objects Strings Lists Matrices
Data frames Functions Many types (often created for packages)
ls() # lists all objects in current workspace
![Page 9: Introduction to R](https://reader036.vdocuments.us/reader036/viewer/2022062305/568164c2550346895dd6d689/html5/thumbnails/9.jpg)
Assignment
= or <- assigns value/data to an object Try this: x <- 5 x phrase <- “hello world” phrase x <- c(2, 3, 4, 5, 9, 10, 11, 12) # any
list of numbers x
![Page 10: Introduction to R](https://reader036.vdocuments.us/reader036/viewer/2022062305/568164c2550346895dd6d689/html5/thumbnails/10.jpg)
hints
R usually ignores spaces Strings belong in quotes
‘string’ “string”
Write your syntax in a file copy&paste Use lots of comments
# comments help you remember next year One workspace per project
(or even one per analysis)
![Page 11: Introduction to R](https://reader036.vdocuments.us/reader036/viewer/2022062305/568164c2550346895dd6d689/html5/thumbnails/11.jpg)
Using functions function(thingToBeFunctionated) mean(x) sd(x) hist(x) (x-mean(x))/sd(x) # z-scoresAssign output of function to an object z <- (x-mean(x))/sd(x) z <- scale(x)
![Page 12: Introduction to R](https://reader036.vdocuments.us/reader036/viewer/2022062305/568164c2550346895dd6d689/html5/thumbnails/12.jpg)
Easy Regression
Example 1 (text file)
![Page 13: Introduction to R](https://reader036.vdocuments.us/reader036/viewer/2022062305/568164c2550346895dd6d689/html5/thumbnails/13.jpg)
Import Data from .csv
Example 2 (text file, pun_dat.csv) Get data into .csv
From Excel or SPSS Save As read.csv()
Puts data into data.frame object (a specialized matrix)
names(data) # I find uppercase names annoying
names(data) <- casefold(names(data))
![Page 14: Introduction to R](https://reader036.vdocuments.us/reader036/viewer/2022062305/568164c2550346895dd6d689/html5/thumbnails/14.jpg)
data.frame objects: indexing Specifying variables within the
data.frame $ data$p_age
Specifying values within the variable []data.frame$variable[value]
data$p_age[10]data.frame[row,col]
data[10,5]
![Page 15: Introduction to R](https://reader036.vdocuments.us/reader036/viewer/2022062305/568164c2550346895dd6d689/html5/thumbnails/15.jpg)
Attaching objects (data frames) attach() makes the data.frame
“available” Reference variables w/o naming the
data.frame Annoying properties
Can attach multiple copies/layers by accident
Changes are not permanent; just a copy Must detach() then attach() for changes to
stick If no planned changes to data…
attach(data)
![Page 16: Introduction to R](https://reader036.vdocuments.us/reader036/viewer/2022062305/568164c2550346895dd6d689/html5/thumbnails/16.jpg)
Some Things Are Easier in R Histogram of number of sex offenders
known hist(numoffsknown) Histogram of transformed variable hist(log(numoffsknown))
![Page 17: Introduction to R](https://reader036.vdocuments.us/reader036/viewer/2022062305/568164c2550346895dd6d689/html5/thumbnails/17.jpg)
Some Things Are Easier in R Histogram of accountability ratings
(SO+NSO)hist(so_nso_acc, col=“#EE0000")
Histogram of undergrad accountability ratingshist(so_nso_acc[which(trt_pro==0)], col="#00EE00")
Histogram of therapist accountability ratingshist(so_nso_acc[which(trt_pro==1)], col="#0000EE85", add=TRUE)
![Page 18: Introduction to R](https://reader036.vdocuments.us/reader036/viewer/2022062305/568164c2550346895dd6d689/html5/thumbnails/18.jpg)
More Regression – GLM
Example 3 (text file) Outcome = number of offenders
known Predictors:
Participant age p_age Treatment professional trt_pro Political affiliation p_politaffil
Use “quasi-poisson” link function
![Page 19: Introduction to R](https://reader036.vdocuments.us/reader036/viewer/2022062305/568164c2550346895dd6d689/html5/thumbnails/19.jpg)
ANOVA
Example 4 (text file) 2x3 ANOVA
Dependent: so_nso_pun overall punishment rating
Factor 1: o_dev_level (child, adolescent, adult)
Factor 2: trt_pro (yes, no) R quirk: must turn factors into “factors”
Basic: lm() and then anova() of the result
Shortcut: aov()
![Page 20: Introduction to R](https://reader036.vdocuments.us/reader036/viewer/2022062305/568164c2550346895dd6d689/html5/thumbnails/20.jpg)
Fun Graphs
Example 5 Scatterplots & correlograms
Example 6: Boxplot with extras
![Page 21: Introduction to R](https://reader036.vdocuments.us/reader036/viewer/2022062305/568164c2550346895dd6d689/html5/thumbnails/21.jpg)
Writing Functions
Becomes easier fairly quickly Extremely useful! This is what lots of people do instead
of downloading packagesExample 7 My function: compute probability that
at least two people in a group of size n will have the same birthday
![Page 22: Introduction to R](https://reader036.vdocuments.us/reader036/viewer/2022062305/568164c2550346895dd6d689/html5/thumbnails/22.jpg)
Other Stuff
Multiple imputation Robust Regression, Loess Curves Factor Analysis… OK? Power analysis ROC curves Really cool and awesome graphs I
don’t need Lots of esoteric analyses I don’t
understand
![Page 23: Introduction to R](https://reader036.vdocuments.us/reader036/viewer/2022062305/568164c2550346895dd6d689/html5/thumbnails/23.jpg)
Disadvantages & Annoyances Yeah, I guess there’s a bit of a learning curve
Eventually you’ll need to start coding Nothing really like SPSS data view
edit() is clunky & problematic Some simple things are ridiculously complex
Beta values in regression Specifying individual values in a dataset Remembering na.rm=TRUE for many functions
Culture of R ≠ psychology culture Documentation is created by & for
mathematicians and computer programmers SEM packages… Not so great (yet)
![Page 24: Introduction to R](https://reader036.vdocuments.us/reader036/viewer/2022062305/568164c2550346895dd6d689/html5/thumbnails/24.jpg)
Some (of Many) Resources
CRAN network: http://cran.revolution-computing.com/
R-project http://www.r-project.org/
R-help mailing list (customer support… kinda) https://stat.ethz.ch/mailman/listinfo/r-help
The R Personality Project (R for psychology) http://www.personality-project.org/r/
![Page 25: Introduction to R](https://reader036.vdocuments.us/reader036/viewer/2022062305/568164c2550346895dd6d689/html5/thumbnails/25.jpg)
More Resources
Quick commands (there are lots of sites like this!) http://www.personality-project.org/r/r.commands.html
Good intro (there are lots of these, too) http://www.personality-project.org/r/book/AppendixA.p
df Quick-R … Awesome resource
http://www.statmethods.net/index.html Jonathan Barron’s R Help Page
http://finzi.psych.upenn.edu/ Another good intro site
http://msenux.redwoods.edu/math/R/
![Page 26: Introduction to R](https://reader036.vdocuments.us/reader036/viewer/2022062305/568164c2550346895dd6d689/html5/thumbnails/26.jpg)
Moar Resources
ANOVA http://www.statmethods.net/stats/anova.ht
ml http://www.personality-project.org/r/r.anov
a.html Graphs/Graphics
http://www.harding.edu/fmccown/R/#misc http://addictedtor.free.fr/graphiques/ http://www.stat.auckland.ac.nz/~paul/RGra
phics/rgraphics.html http://zoonek2.free.fr/UNIX/48_R/03.html
![Page 27: Introduction to R](https://reader036.vdocuments.us/reader036/viewer/2022062305/568164c2550346895dd6d689/html5/thumbnails/27.jpg)
Yet more
Robust Regression http://www.ats.ucla.edu/stat/r/dae/rreg.ht
m Writing Functions
http://fs6.depauw.edu:50080/~harvey/Chem%20351/PDF%20Files/Handouts/RDocs/Writing%20Functions%20Using%20R.pdf
GLMs (e.g., logistic, poisson) http://data.princeton.edu/R/glms.html http://www.stanford.edu/class/stats306a/RforG
LM.pdf http://www.jstatsoft.org/v08/i15/paper
![Page 28: Introduction to R](https://reader036.vdocuments.us/reader036/viewer/2022062305/568164c2550346895dd6d689/html5/thumbnails/28.jpg)
How to Get Help
From within R RSiteSearch(“xxx”) ?xxxx or help(“xxx”)
Google “R-Help xxx” “R package xxx” “R how-to xxx”