Skip to content

Instantly share code, notes, and snippets.

View jungsy8745's full-sized avatar

Seyoung Jung jungsy8745

View GitHub Profile
@jungsy8745
jungsy8745 / dailyr74.stat
Created December 6, 2016 19:30
ggplot practice
# 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
@jungsy8745
jungsy8745 / dailyr73.stat
Created December 6, 2016 03:25
ggplot practice
# 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"))
@jungsy8745
jungsy8745 / dailyr74.stat
Last active December 4, 2016 05:26
ez package
#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
@jungsy8745
jungsy8745 / dailyr73.stat
Last active December 4, 2016 05:26
psych package
#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
@jungsy8745
jungsy8745 / dailyr72.stat
Last active December 4, 2016 05:26
create a new data
#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)
@jungsy8745
jungsy8745 / dailyr71.stat
Created December 2, 2016 05:18
add a column and a row to a dataset
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
@jungsy8745
jungsy8745 / dailyr70.stat
Created December 2, 2016 05:02
permutation test
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])
@jungsy8745
jungsy8745 / dailyr74.stat
Created November 23, 2016 05:15
recoding
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
@jungsy8745
jungsy8745 / dailyr73.stat
Last active November 21, 2016 22:01
regression table
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")
@jungsy8745
jungsy8745 / dailyr72.stat
Created November 20, 2016 05:28
tree plot
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)