Introduction to R Thomas J. Leeper Department of Political Science - - PowerPoint PPT Presentation

introduction to r
SMART_READER_LITE
LIVE PREVIEW

Introduction to R Thomas J. Leeper Department of Political Science - - PowerPoint PPT Presentation

The R Language Introduction to R Thomas J. Leeper Department of Political Science and Government Aarhus University November 14, 2013 The R Language The R Language The R Language 1 The R Language The R Language 1 The R Language Try on


slide-1
SLIDE 1

The R Language

Introduction to R

Thomas J. Leeper

Department of Political Science and Government Aarhus University

November 14, 2013

slide-2
SLIDE 2

The R Language

slide-3
SLIDE 3

The R Language

1

The R Language

slide-4
SLIDE 4

The R Language

1

The R Language

slide-5
SLIDE 5

The R Language

Try on your own

Use R as a calculator: Do the “Basic Math” tutorial

slide-6
SLIDE 6

The R Language

Tips

Always know where you are with getwd()

slide-7
SLIDE 7

The R Language

Tips

Always know where you are with getwd() R is case-sensitive: mean is not the same as Mean

slide-8
SLIDE 8

The R Language

Tips

Always know where you are with getwd() R is case-sensitive: mean is not the same as Mean ⇑ and ⇓ cycle through your command history

slide-9
SLIDE 9

The R Language

Tips

Always know where you are with getwd() R is case-sensitive: mean is not the same as Mean ⇑ and ⇓ cycle through your command history Page Up and Page Down control your screen

slide-10
SLIDE 10

The R Language

Tips

Always know where you are with getwd() R is case-sensitive: mean is not the same as Mean ⇑ and ⇓ cycle through your command history Page Up and Page Down control your screen > prompt means R is ready for a command + means your last command wasn’t complete

slide-11
SLIDE 11

The R Language

Questions so far?

slide-12
SLIDE 12

The R Language

Your first analysis in R

Load data Estimate 4 regression models Look at coefficients

slide-13
SLIDE 13

The R Language

Your first analysis in R

Load data Estimate 4 regression models Look at coefficients Data from: Cusack, Iversen, Soskice. (2007). “Economic Interests and the Origins of Electoral Systems” APSR 101(3):373–391.

slide-14
SLIDE 14

The R Language

Your first analysis in R

slide-15
SLIDE 15

The R Language

Your first analysis in R

library("foreign") cis <- read.dta(file.choose())[cis$smpl==1,] lm3_1 <- lm(thresh~threat + fragdum, data = cis) lm3_2 <- lm(thresh~threat + fragdum, data=cis[cis$oursmpl==1,]) lm3_3 <- lm(thresh~threat13 + fragdum, data=cis[cis$oursmpl==1,]) lm3_4 <- lm(thresh~stthroct2 + fragdum, data=cis[cis$oursmpl==1,]) lapply(ls(pattern="lm"), function(i) round(summary(get(i))$coef[,1:2],2))

slide-16
SLIDE 16

The R Language

Your first analysis in R

library("foreign") cis <- read.dta(file.choose())[cis$smpl==1,] lm3_1 <- lm(thresh~threat + fragdum, data = cis) lm3_2 <- lm(thresh~threat + fragdum, data=cis[cis$oursmpl==1,]) lm3_3 <- lm(thresh~threat13 + fragdum, data=cis[cis$oursmpl==1,]) lm3_4 <- lm(thresh~stthroct2 + fragdum, data=cis[cis$oursmpl==1,]) lapply(ls(pattern="lm"), function(i) round(summary(get(i))$coef[,1:2],2))

slide-17
SLIDE 17

The R Language

Your first analysis in R

library("foreign") cis <- read.dta(file.choose())[cis$smpl==1,] lm3_1 <- lm(thresh~threat + fragdum, data = cis) lm3_2 <- lm(thresh~threat + fragdum, data=cis[cis$oursmpl==1,]) lm3_3 <- lm(thresh~threat13 + fragdum, data=cis[cis$oursmpl==1,]) lm3_4 <- lm(thresh~stthroct2 + fragdum, data=cis[cis$oursmpl==1,]) lapply(ls(pattern="lm"), function(i) round(summary(get(i))$coef[,1:2],2))

slide-18
SLIDE 18

The R Language

Your first analysis in R

library("foreign") cis <- read.dta(file.choose())[cis$smpl==1,] lm3_1 <- lm(thresh~threat + fragdum, data = cis) lm3_2 <- lm(thresh~threat + fragdum, data=cis[cis$oursmpl==1,]) lm3_3 <- lm(thresh~threat13 + fragdum, data=cis[cis$oursmpl==1,]) lm3_4 <- lm(thresh~stthroct2 + fragdum, data=cis[cis$oursmpl==1,]) lapply(ls(pattern="lm"), function(i) round(summary(get(i))$coef[,1:2],2))

slide-19
SLIDE 19

The R Language

Your first analysis in R

library("foreign") cis <- read.dta(file.choose())[cis$smpl==1,] lm3_1 <- lm(thresh~threat + fragdum, data = cis) lm3_2 <- lm(thresh~threat + fragdum, data=cis[cis$oursmpl==1,]) lm3_3 <- lm(thresh~threat13 + fragdum, data=cis[cis$oursmpl==1,]) lm3_4 <- lm(thresh~stthroct2 + fragdum, data=cis[cis$oursmpl==1,]) lapply(ls(pattern="lm"), function(i) round(summary(get(i))$coef[,1:2],2))

slide-20
SLIDE 20

The R Language

Your first analysis in R

library("foreign") cis <- read.dta(file.choose())[cis$smpl==1,] lm3_1 <- lm(thresh~threat + fragdum, data = cis) lm3_2 <- lm(thresh~threat + fragdum, data=cis[cis$oursmpl==1,]) lm3_3 <- lm(thresh~threat13 + fragdum, data=cis[cis$oursmpl==1,]) lm3_4 <- lm(thresh~stthroct2 + fragdum, data=cis[cis$oursmpl==1,]) lapply(ls(pattern="lm"), function(i) round(summary(get(i))$coef[,1:2],2))

slide-21
SLIDE 21

The R Language

Your first analysis in R

library("foreign") cis <- read.dta(file.choose())[cis$smpl==1,] lm3_1 <- lm(thresh~threat + fragdum, data = cis) lm3_2 <- lm(thresh~threat + fragdum, data=cis[cis$oursmpl==1,]) lm3_3 <- lm(thresh~threat13 + fragdum, data=cis[cis$oursmpl==1,]) lm3_4 <- lm(thresh~stthroct2 + fragdum, data=cis[cis$oursmpl==1,]) lapply(ls(pattern="lm"), function(i) round(summary(get(i))$coef[,1:2],2))

slide-22
SLIDE 22

The R Language

Your first analysis in R

library("foreign") cis <- read.dta(file.choose())[cis$smpl==1,] lm3_1 <- lm(thresh~threat + fragdum, data = cis) lm3_2 <- lm(thresh~threat + fragdum, data=cis[cis$oursmpl==1,]) lm3_3 <- lm(thresh~threat13 + fragdum, data=cis[cis$oursmpl==1,]) lm3_4 <- lm(thresh~stthroct2 + fragdum, data=cis[cis$oursmpl==1,]) lapply(ls(pattern="lm"), function(i) round(summary(get(i))$coef[,1:2],2))

slide-23
SLIDE 23

The R Language

Questions so far?

slide-24
SLIDE 24

The R Language

R Language Basics

Everything in R is an “object”

slide-25
SLIDE 25

The R Language

Try on your own

Create variables: Do the “Variables” tutorial

slide-26
SLIDE 26

The R Language

Questions so far?

slide-27
SLIDE 27

The R Language

R Language Basics

Everything in R is an “object”

slide-28
SLIDE 28

The R Language

R Language Basics

Everything in R is an “object” All objects have a “class”

slide-29
SLIDE 29

The R Language

Try on your own

Understand R data structures: “Vectors” “Vector indexing” “Matrices” “Lists” “Dataframes”

slide-30
SLIDE 30

The R Language

Questions so far?

slide-31
SLIDE 31

The R Language

R Language Basics

Everything in R is an “object” All objects have a “class”

slide-32
SLIDE 32

The R Language

R Language Basics

Everything in R is an “object” All objects have a “class” We execute functions on objects

Functions work differently (or not at all) on different classes

slide-33
SLIDE 33

The R Language

R Language Basics

Everything in R is an “object” All objects have a “class” We execute functions on objects

Functions work differently (or not at all) on different classes

Functions return objects and print to the console

slide-34
SLIDE 34

The R Language

R Language Basics

Everything in R is an “object” All objects have a “class” We execute functions on objects

Functions work differently (or not at all) on different classes

Functions return objects and print to the console We can then do further things with those objects

slide-35
SLIDE 35

The R Language

Try on your own

Understand objects and printing: Do the “Objects” tutorial

slide-36
SLIDE 36

The R Language

When things go wrong...

slide-37
SLIDE 37

The R Language

When things go wrong...

1 Don’t panic 2 Parsing errors versus syntax errors:

Error: unexpected ’)’ in "lm(y ~)" Error in eval(expr, envir, enclos) :

  • bject ’y’ not found

3 Google the error or warning 4 Use StackOverflow

slide-38
SLIDE 38

The R Language

A script editor makes your life easier...

Helps you understand code more easily Work interactively to build a script

R script is like a Stata do file File extension: .R or .r

Write down all of your code Use comments (#) liberally

slide-39
SLIDE 39

The R Language

...so let’s install one

install.packages("devtools") library("devtools") install_github("rite", "leeper") library("rite") rite()

slide-40
SLIDE 40

The R Language

...so let’s install one

install.packages("devtools") library("devtools") install_github("rite", "leeper") library("rite") rite()

slide-41
SLIDE 41

The R Language

...so let’s install one

install.packages("devtools") library("devtools") install_github("rite", "leeper") library("rite") rite()

slide-42
SLIDE 42

The R Language

...so let’s install one

install.packages("devtools") library("devtools") install_github("rite", "leeper") library("rite") rite()

slide-43
SLIDE 43

The R Language

...so let’s install one

install.packages("devtools") library("devtools") install_github("rite", "leeper") library("rite") rite()

slide-44
SLIDE 44

The R Language

...so let’s install one

install.packages("devtools") library("devtools") install_github("rite", "leeper") library("rite") rite()

slide-45
SLIDE 45

The R Language

Questions so far?

slide-46
SLIDE 46