Linear Regression in Julia 1.0

Julia presents various ways to carry out linear regressions. In this previous post, I explained how to run linear regression in Julia using the function linreg(). Unfortunately, linreg() is deprecated and no longer exists in Julia v1.0.

In this post I will present how to use the native function of Julia to run OLS on the following model

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

An alternative way to run a linear regression is to use the lm() function of the GLM package. In case you are interested in running a regression based on the GLM package, you can check out this post. It describes how to conduct a multiple regression in Julia and uses the lm() function provided by the GLM package.

In this example, 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. For a more detailed description of the data see here.

#using DataArrays
# load Taro - Pkg to read Excel Data
using Taro
Taro.init()

# get data
path="https://economictheoryblog.files.wordpress.com/2016/08/data.xlsx"
data = Taro.readxl(download(path), "data", "A1:C357")

using DataFrames
data = DataFrame(data)
deleterows!(data,findall(ismissing,data[:,1]))
deleterows!(data,findall(ismissing,data[:,2]))
y = convert(Array{Float64,1},data[:,1])
x = convert(Array{Float64,1},data[:,2])
    
reverse([x ones(length(x))]\y)

The function reverse() returns point estimates for \alpha and \beta_{1}. Unfortunately, the function does not return standard error for the point estimates. In order to obtain standard errors for the estimated coefficients, one can use the lm() function form the GLM package. The GLM package of Julia does provide a more flexible environment. You can find a working example of how to conduct multiple regression in Julia using the GLM package here.

Advertisements

One thought on “Linear Regression in Julia 1.0”

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 )

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.