Using RStudio to do OLS analysis
R provide convenient packages for us to do econometric analysis. We can put all the command into a handy R script,and reuse it again.
First, load ‘foreign’ library if your data is export from STATA, SPSS and EXCEL. Alternatively, you could use RODBC to load excel, etc.
Then set the work directory with ‘setwd’ command, type in ‘read.dta’ to import data.
Using ’cbind’ to strip and combine the variables you want to explore.
In order to confirm the correction among variables, we could plot the image of the relationship of the variables using ‘plot’.
Or calculate correlation using command ‘cor’.
Finally is the step for inference. We use ‘lm’ command to estimate the coefficients, type ‘summary()’ to see the results.
Below is the whole code of the scripts.
library(foreign) # import dataframe, use package 'foreigh' # RODBC(excel), Hmisc(SPSS, SAS), R.matlab setwd("/Users/your/Documents/data") cdata = read.dta("BWGHT.dta") #for csv, use read.csv # define variabes y <- cbind(bwght) x <- cbind(cigs) # describetive analysis plot(y~x, data = cdata) cor(y,x) # inference olsreg <- lm(y ~ x) summary(olsreg) confint(olsreg, level=0.95) # anova(olsreg)













