introduction to r

Post on 23-Feb-2016

39 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

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 Presentation

TRANSCRIPT

INTRODUCTION TO R

Darrin Rogers (not any kind of R expert guy)Department of Psychology & AnthropologyUniversity of Texas – Pan American

4/14/2011

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

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.

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

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

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

R basics Use a text editor

I like notepad++ (“R aware”) Notepad works OK

Case-sensitive! Psych ≠ psych

Help ?something ??something RSiteSearch(“something”) google

Objects

Many kinds of objects Strings Lists Matrices

Data frames Functions Many types (often created for packages)

ls() # lists all objects in current workspace

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

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)

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)

Easy Regression

Example 1 (text file)

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

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]

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)

Some Things Are Easier in R Histogram of number of sex offenders

known hist(numoffsknown) Histogram of transformed variable hist(log(numoffsknown))

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)

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

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

Fun Graphs

Example 5 Scatterplots & correlograms

Example 6: Boxplot with extras

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

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

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)

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/

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/

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

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

top related