Created
December 5, 2014 14:10
-
-
Save cigrainger/ed51eff3d96a3753a7f3 to your computer and use it in GitHub Desktop.
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
install.packages('stargazer') | |
# states <- state.name | |
states <- rep(state.name,2) | |
# states <- rep(2,state.name) | |
# states1 <- rep(x=state.name,times=2) | |
# states2 <- rep(times=2,x=state.name) | |
# states1==states2 | |
variable <- c(rep('x',50),rep('y',50)) | |
# Two vectors (2010,2011) of length 100, | |
# composed of random numbers between 1 and 1000. | |
X2010 <- sample(1:1000,100) | |
X2011 <- sample(500:1000,100) | |
df <- data.frame(X2010,X2011,states,variable) | |
df1 <- df[df$variable=='x',] | |
df2 <- df[df$variable=='y',] | |
df <- rbind(df1,df2) | |
# Generate a vector (X2012) of length 100 with random | |
# values taken from a normal distribution with mean 100, | |
# and a standard deviation of 10. | |
X2012 <- rnorm(100,mean=100,sd=10) | |
df <- cbind(df,X2012) | |
# Make df$X2012 an integer vector by rounding to whole | |
# numbers. | |
df$X2012 <- as.integer(df$X2012) | |
# Change df to long form like on the board. Don't forget | |
# to change the year values so that X2012 equals | |
# 2012, X2010 equals 2010 etc. Use melt and cast (dcast) | |
# in reshape2. Look at last week's work or google. Check | |
# the structure (str()) of the final data frame to make | |
# sure everything makes sense (year should be integer). | |
# Ways to change X2012 to 2012 -- substr() or gsub(). | |
df <- melt(df,id.vars=c('states','variable')) | |
# names(df) <- c('states','variable','year','value') | |
names(df)[3] <- 'year' | |
df$year <- substr(df$year,2,5) | |
# df$year <- gsub('X','',x=df$year) | |
df <- dcast(df,states + year ~ variable) | |
df$x <- as.integer(df$x) | |
df$y <- as.integer(df$y) | |
hist(df$x) | |
library(ggplot2) | |
ggplot(data=df,aes(x=x,y=y)) + geom_point() | |
m <- lm(y ~ x,data=df) | |
summary(m) | |
install.packages('stargazer') | |
library(stargazer) | |
stargazer(m,type='html',out='test.html') | |
stargazer(m,type='text') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment