Skip to content

Instantly share code, notes, and snippets.

@KristofferC
Created August 23, 2015 19:33
Show Gist options
  • Save KristofferC/f424e1d7daf4c6496ad2 to your computer and use it in GitHub Desktop.
Save KristofferC/f424e1d7daf4c6496ad2 to your computer and use it in GitHub Desktop.
using DataFrames
using DataFramesMeta
using GLM
function f()
DPY = 252 ## days per year
NWINDOW = 126 ## can be smaller or larger than 252
ds = readtable("xri.csv") ## a sample data set
# create two empty arrays to store b_ols and sd_ols value
b_ols = DataArray(Float64, size(ds)[1])
sd_ols = DataArray(Float64, size(ds)[1])
for i = 1:size(ds)[1]
thisDay = ds[i, :day] ## Julia DataFrame way of accessing data, in R: ds$day[i]
if mod(thisDay, DPY) != 0
continue
end
if thisDay < DPY
continue
end
thisFm = ds[i, :fm]
dataSubset = @where(ds, (:fm .== thisFm) & (:day .>= (thisDay - NWINDOW)) & (:day .<= (thisDay - 1)))
olsReg = fit(LinearModel, xr ~ xm, dataSubset) ## OLS from package GLM
b_ols[i] = coef(olsReg)[2] ## returns the OLS coefficients
sd_ols[i] = stderr(olsReg)[2] ## returns the OLS coefficients' standard error
print(i, " ")
end
ds[:b_ols] = b_ols
ds[:sd_ols] = sd_ols
print("\nOLS Beta Regressions are Done\n")
return ds
end
ds = readtable("xri.csv")
@time f(ds)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment