1 / 28

Introduction to R

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)

enye
Download Presentation

Introduction to R

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Darrin Rogers (not any kind of R expert guy) Department of Psychology & Anthropology University of Texas – Pan American 4/14/2011 Introduction to R

  2. Installing R Do this while I’m rambling on about R… • http://cran.revolution-computing.com/ • (CRAN mirror in San Antonio) • Installationfileis 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

  3. 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.

  4. 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 • Severalincluded in “basicinstall” • About 3,000 availablethrough CRAN • FREE & open-source

  5. 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

  6. 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

  7. R basics • Use a text editor • I like notepad++ (“R aware”) • Notepad works OK • Case-sensitive! • Psych ≠ psych • Help ?something ??something RSiteSearch(“something”) • google

  8. Objects • Many kinds of objects • Strings • Lists • Matrices • Data frames • Functions • Many types (often created for packages) ls() # lists all objects in current workspace

  9. 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

  10. 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)

  11. Using functions • function(thingToBeFunctionated) mean(x) sd(x) hist(x) (x-mean(x))/sd(x)# z-scores Assign output of function to an object z <- (x-mean(x))/sd(x) z <- scale(x)

  12. Easy Regression • Example 1 (text file)

  13. 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))

  14. 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]

  15. 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)

  16. Some Things Are Easier in R • Histogram of number of sex offenders known hist(numoffsknown) • Histogram of transformed variable hist(log(numoffsknown))

  17. Some Things Are Easier in R • Histogram of accountability ratings (SO+NSO) hist(so_nso_acc, col=“#EE0000") • Histogram of undergrad accountability ratings hist(so_nso_acc[which(trt_pro==0)], col="#00EE00") • Histogram of therapist accountability ratings hist(so_nso_acc[which(trt_pro==1)], col="#0000EE85", add=TRUE)

  18. 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

  19. 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()

  20. Fun Graphs Example 5 • Scatterplots & correlograms Example 6: • Boxplot with extras

  21. Writing Functions • Becomes easier fairly quickly • Extremely useful! • This is what lots of people do instead of downloading packages Example 7 • My function: compute probability that at least two people in a group of size n will have the same birthday

  22. 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

  23. 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 • Rememberingna.rm=TRUE formanyfunctions • Culture of R ≠ psychologyculture • Documentation is created by & for mathematicians and computer programmers • SEM packages… Not so great (yet)

  24. 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/

  25. 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.pdf • 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/

  26. Moar Resources • ANOVA • http://www.statmethods.net/stats/anova.html • http://www.personality-project.org/r/r.anova.html • Graphs/Graphics • http://www.harding.edu/fmccown/R/#misc • http://addictedtor.free.fr/graphiques/ • http://www.stat.auckland.ac.nz/~paul/RGraphics/rgraphics.html • http://zoonek2.free.fr/UNIX/48_R/03.html

  27. Yet more • Robust Regression • http://www.ats.ucla.edu/stat/r/dae/rreg.htm • 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/RforGLM.pdf • http://www.jstatsoft.org/v08/i15/paper

  28. How to Get Help • From within R • RSiteSearch(“xxx”) • ?xxxx or help(“xxx”) • Google • “R-Help xxx” • “R package xxx” • “R how-to xxx” drogers1@utpa.edu

More Related