Skip to content

Instantly share code, notes, and snippets.

@KristofferC
Last active August 29, 2015 14:28
Show Gist options
  • Save KristofferC/207dcc6f4f22eed73562 to your computer and use it in GitHub Desktop.
Save KristofferC/207dcc6f4f22eed73562 to your computer and use it in GitHub Desktop.
using DataFrames
using DataFramesMeta
using GLM
using Base.Collections
function f(ds)
DPY = 252 ## days per year
NWINDOW = 126 ## can be smaller or larger than 252
FMCOL = 1
DAYCOL = 2
# 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]
# Find the rows that fits with given Fm
cands_fm = searchsorted(ds[FMCOL].data, thisFm)
# Find the last index where the day is >= thisDay - 1
start_days = searchsortedfirst(ds[DAYCOL].data, thisDay - NWINDOW,
cands_fm[1], cands_fm[end], Forward)
# Find the first index where day is < thisDay - 1
end_days = searchsortedlast(ds[DAYCOL].data, thisDay - 1,
cands_fm[1], cands_fm[end], Forward)
# Extract subset from data
dataSubset = ds[start_days:end_days, :]
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") ## a sample data set
@time f(ds);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment