I got this example working in XRJulia.
XRJulia example
Now I want to modify it so the Julia code is in a separate module, but I’m having a very unsuccessful time of it. I made a file called combin.jl
(see below). Can’ anyone tell me how to import these two functions into R now? I thought the following was working but seems it’s not.
> library(XRJulia)
> library(HDeconometrics)
> findJulia()
[1] "C:\\Users\\WoodwardS\\AppData\\Local\\JuliaPro-0.6.2.1\\Julia-0.6.2\\bin\\julia.exe"
> juliaAddToPath(getwd(), "combin.jl") # tell Julia to looks for modules here
[1] TRUE
> # find function reg() in combin.jl
> regjl <- juliaEval("reg", "combin.jl")
Error: lexical error: invalid char in json text.
NA
(right here) ------^
This is combin.jl
:
module combin
using Combinatorics
export reg, csr
function reg(x,y)
n=size(x,1)
xreg=hcat(ones(size(x)[1],1),x)
k=size(xreg,2)
p1=((xreg'xreg)^(-1))
b=p1*xreg'y
r=y-xreg*b
sig=(r'r)/(n-k)
vmat=sig[1]*p1
sigb=sqrt.(diag(vmat))
t=b./sigb
return (b,t)
end
function csr(x,y,k,K,fixed_controls)
fixed_controls=floor(Int,fixed_controls)
if fixed_controls!=0
w=x[:,fixed_controls]
nonw=setdiff(1:size(x,2),fixed_controls)
end
if fixed_controls[1]==0
nonw=1:size(x,2)
end
save_stat=zeros(size(x,2),2)
save_stat[:,2]=1:size(x,2)
for i in nonw
if fixed_controls[1]==0
(betas,tstat)=reg(x[:,i],y)
save_stat[i,1]=abs(tstat[2])
end
if fixed_controls!=0
(betas,tstat)=reg(hcat(w,x[:,i]),y)
save_stat[i,1]=abs(tstat[end])
end
end
t_ord = sortperm(save_stat[:,1],rev=true)
save_stat=save_stat[t_ord,:]
selected=save_stat[1:K,2]
comb=collect(Combinatorics.combinations(selected,k))
m=size(comb)[1]
final_coef=zeros(m,size(x,2))
final_const=zeros(m)
if fixed_controls!=0
for i in 1:m
xr=hcat(x[:,fixed_controls],x[:,floor(Int,comb[i])])
(model,tstat)=reg(xr,y)
final_const[i]=model[1]
model1=model[2:end]
if length(fixed_controls)>1
final_coef[i,fixed_controls]=model1[1:(size(fixed_controls)[1])]
model2=model1[size(fixed_controls)[1]+1:end]
else
final_coef[i,fixed_controls]=model1[1]
model2=model1[2:end]
end
final_coef[i,floor(Int,comb[i])]=model2
end
elseif fixed_controls[1]==0
for i in 1:m
(model,tstat)=reg(x[:,floor(Int,comb[i])],y)
final_const[i]=model[1]
final_coef[i,floor(Int,comb[i])]=model[2:end]
end
end
aux=hcat(final_const,final_coef)
result=[mean(aux[:,i]) for i in 1:size(aux)[2]]
end
end