Introductory R Code

###################################################################################

# INTRODUCTORY R CODE

########################################
## download both R and R Studio; work out of R Studio
## download csv file -or- convert/export spreadsheet to .csv file

## R Studio is pretty customizable if you prefer larger code workspace or larger
## console, etc, swap quadrants of workspace, or change colors in source file
# window for viewing ease, etc:
## From MENU: -> TOOLS -> GLOBAL OPTIONS -> APPEARANCE
## at first, maybe try just copy/pasting variable names then messing around
## with parameters/options
## Quick R and Stackoverflow are good resources

## control + enter when curser is on line to execute code in console
## control+shift C to comment out source code (#)
##################################################################################
## LOADING DATA AND VARIOUS BASIC ANALYSIS

## set working directory

## From MENU: -> SESSION -> SET WORKING DIRECTORY -> TO SOURCE FILE LOCATION
## — OR
setwd(“C:/Users/SPACELOSS/Dropbox/KR & dA”)
## load data from .csv file (convert/export spreadsheet to .csv file)
## give data set (file) a name
## view variables

Data attach(Data)
names(Data)

# returns variable names:

# [1] “Year” “apollo_era” “Space_Cong”
# [4] “Space_Budg” “Percent_Chg”

#######################################################

## Dependent Variable Analysis

################################################
## nickname variable – “Space_Budg” to “DV” for ease

DV

# if needing to specify which dataset variable came from in case of multiple sets
DV

# if need to subset data by Year, etc and remove NA’s

DV49 = 1949, na.rm=TRUE)

## create density plot then show in workspace
d1 plot(d1)
#histogram & set breaks
h1 <-hist(DV, breaks=40)
#plot
plot(h1)
# add a fit of normal distribution using data mean (mu),
## and standard deviation (sigma); color – darkblue; line width = 2
d2 Space_Cong
## y -> ABSpace_BudgLag
reg1 summary(reg1)
confint(reg1, level=0.95)

# add control variables with + DV2 + DV3, etc
reg1a summary(reg1a)

###########

## download ‘stargazer’ package for LaTeX code output of pro tables (only once)
install.packages(“stargazer”)

## load package every session
library(stargazer)

# should return LaTeX code
stargazer(reg1)

# Looks like:

# % Table created by stargazer v.5.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
# % Date and time: Sat, Aug 06, 2016 – 6:51:19 AM
# \begin{table}[!htbp] \centering
# \caption{}
# etc…
####################################################
## plot customized regression with error margins, labels, etc.

# Plot of Regression 1 – reg1

par(mar=c(5, 5, 4, 5) + 0.1)
newx 0.01)
a interval=”confidence”)
plot Spending Change: 1949-2013″, axes=FALSE, xlab=”Congressional Attention”,
ylab=””, col=’orangered’)
mtext(“Spending Change”, side=2, line=4.0)
box()
axis(2, at=pretty(ABSpace_BudgLag), lab=paste0(pretty(ABSpace_BudgLag) * 100,
” %”), las=1)
axis(1, at=pretty(Space_Cong), lab=paste0(pretty(Space_Cong) * 100, ” %”),
ylim=c(0,.1), las=1)

## add regression line
abline(lm(ABSpace_BudgLag~Space_Cong), col=”darkmagenta”)

## add error margins of regression line
lines(newx,a[,2], lty=3, type=”c”, col=”blue”)
lines(newx,a[,3], lty=3, type=”c”, col=”blue”)
lines(newx,a[,2], lty=3, type=”l”, col=”blue”)
lines(newx,a[,3], lty=3, type=”l”, col=”blue”)

###########################################################################3
### SPACELOSS

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s