This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ggplot2 examples | |
library(ggplot2) | |
# create factors with value labels | |
mtcars$gear <- factor(mtcars$gear,levels=c(3,4,5), | |
labels=c("3gears","4gears","5gears")) | |
mtcars$am <- factor(mtcars$am,levels=c(0,1), | |
labels=c("Automatic","Manual")) | |
mtcars$cyl <- factor(mtcars$cyl,levels=c(4,6,8), | |
labels=c("4cyl","6cyl","8cyl")) | |
# Separate regressions of mpg on weight for each number of cylinders |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ggplot2 examples | |
library(ggplot2) | |
# create factors with value labels | |
mtcars$gear <- factor(mtcars$gear,levels=c(3,4,5), | |
labels=c("3gears","4gears","5gears")) | |
mtcars$am <- factor(mtcars$am,levels=c(0,1), | |
labels=c("Automatic","Manual")) | |
mtcars$cyl <- factor(mtcars$cyl,levels=c(4,6,8), | |
labels=c("4cyl","6cyl","8cyl")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#create a data | |
library(gsheet) | |
url <- 'https://docs.google.com/spreadsheets/d/14I19f3hof7A83W62fZTbsFuMMcpL-vNTwQaJrt64Rpc/edit?usp=sharing' | |
exercise <- gsheet2tbl(url) | |
library(foreign) | |
attach(exercise) | |
#Perform a repeated measures analysis with only the within subjects factor | |
library(ez) | |
ex1 = ezANOVA(exercise,dv = .(pulse), wid = .(id), within = .(time),detailed = TRUE) | |
ex1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#create a data | |
library(gsheet) | |
url <- 'https://docs.google.com/spreadsheets/d/14I19f3hof7A83W62fZTbsFuMMcpL-vNTwQaJrt64Rpc/edit?usp=sharing' | |
exercise <- gsheet2tbl(url) | |
library(foreign) | |
attach(exercise) | |
#Generate descriptive statistics for the sex variable which is a between subjects factor | |
library(psych) | |
describeBy(pulse,diet) | |
#Generate descriptive statistics for the treatment level variable which is a between subjects factor |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#create a new data | |
library(gsheet) | |
url <- 'https://docs.google.com/spreadsheets/d/14I19f3hof7A83W62fZTbsFuMMcpL-vNTwQaJrt64Rpc/edit?usp=sharing' | |
exercise <- gsheet2tbl(url) | |
library(foreign) | |
attach(exercise) | |
#Check for missing values | |
sapply(exercise, function(x) sum(is.na(x))) | |
#Check for balance in between-subjects factor | |
table(diet) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trees | |
a=c(70, 65, 63, 72, 80, 83, 66, 75, 80, 75, 79, 76, 76, 69, 75, 74, 85, 8, 71, 63, 78, 80, 74, 72, 77, 81, 82, 80, 86, 80, 87) | |
b=c(21,90,70,80) | |
trees=cbind(trees,a) | |
trees=rbind(trees,b) | |
trees |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(gsheet) | |
url <- 'https://docs.google.com/spreadsheets/d/14I19f3hof7A83W62fZTbsFuMMcpL-vNTwQaJrt64Rpc/edit?usp=sharing' | |
a <- gsheet2tbl(url) | |
a$sF<-factor(a$Pair) | |
t.test (subset(a, Treatment==1)$Outcome,subset(a, Treatment==0)$Outcome, paired=TRUE) | |
library(randomizr) | |
meandiffTZ<-function(Y,Z,S){ | |
Ymd <- Y - ave(Y,S) | |
result <- mean(Ymd[Z==1]) - mean(Ymd[Z==0]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
download.file("http://jakebowers.org/Data/ANES/anes_pilot_2016_csv.zip",destfile="anespilot2016.csv.zip") | |
unzip("anespilot2016.csv.zip") | |
anespi16<-read.csv("anes_pilot_2016.csv",as.is=TRUE,strip.white=TRUE) | |
#before recoding | |
table(anespi16$pid3) | |
#recode Party Id for Republicans | |
anespi16$partyrep <- ifelse(anespi16$pid3 == 1, 1, 0) | |
#recode Party Id for Democrats | |
anespi16$partydem <- ifelse(anespi16$pid3 == 2, 1, 0) | |
#recode Party Id for Independents |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
download.file("http://jakebowers.org/Data/ANES/anes_pilot_2016_csv.zip",destfile="anespilot2016.csv.zip") | |
unzip("anespilot2016.csv.zip") | |
anespi16<-read.csv("anes_pilot_2016.csv",as.is=TRUE,strip.white=TRUE) | |
attach(anespi16) | |
fit<- lm(turnout12~lcself) | |
library(stargazer) | |
stargazer::stargazer(fit, type="text") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tree) | |
iris.tr <- tree(Species~., iris) | |
plot(iris.tr) | |
text(iris.tr, all=T) | |
iris.tr2 <- prune.misclass(iris.tr, best=4) | |
plot(iris.tr2) | |
text(iris.tr2, all=T) |
NewerOlder