Linear Regression in Julia

Julia presents various ways to carry out linear regressions. One easy way is to use the lm() function of the GLM package. In this post I will present how to use the lm() and run OLS on the following model

y = \alpha + \beta_{1} x_{1} + \beta_{2} x_{2} + \beta_{3} x_{3}

Our dependent variable y will be my weekly average weight, the explanatory variable x_{1} represents the sum of calories that I burned during the previous week, and variable latex x_{2}$ is a binary variable that takes a value of 1 in case I was cycling the week earlier and 0 otherwise. For a more detailed description of the data see here.

# load a couple of packages
using Distributions
using GLM
using DataFrames
using DataArrays
# load Taro - Pkg to read Excel Data
using Taro

# get data
path = ""
data = Taro.readxl(download(path), "data", "A1:C357")
data = deleterows!(data,find(isna(data[:,1])|isna(data[:,2])))
data[:,1] = convert(DataArrays.DataArray{Float64,1},data[:,1])
data[:,2] = convert(DataArrays.DataArray{Float64,1},data[:,2])
data[:,3] = convert(DataArrays.DataArray{Float64,1},data[:,3])

# estimate the linear regression model
glm(@formula(weight ~ lag_calories + lag_cycling),
        data, Normal(), IdentityLink())

This entry was posted in Computing and Others, Econometrics and tagged , , , . Bookmark the permalink.

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s