Skip to content

Commit

Permalink
Try julia -e with single as well as double quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
droodman committed Dec 9, 2023
1 parent 5ca8c27 commit b4ea020
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 54 deletions.
39 changes: 0 additions & 39 deletions jresidualize.ado

This file was deleted.

96 changes: 96 additions & 0 deletions partialhdfejl.ado
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Perform HDFE partialling in a common sample
cap program drop partialhdfejl
program define partialhdfejl
syntax varlist [if] [in] [aw pw fw iw/], Absorb(string) [GENerate(string) PREfix(string) replace]

if (`"`generate'"'!="") + (`"`prefix'"'!="") != 1 {
di as err "Specify exactly one of {cmdab:gen:erate()} or {cmdab:pre:fix()}."
exit 198
}

marksample touse

if strpos("`varlist'", ".") | strpos("`varlist'", "#") | strpos("`varlist'", "-") | strpos("`varlist'", "?") | strpos("`varlist'", "*") | strpos("`varlist'", "~") {
fvexpand `varlist' if `touse'
local varlist
foreach var in `r(varlist)' {
_ms_parse_parts `var'
if !r(omit) local varlist `varlist' `var'
}
fvrevar `varlist' if `touse'
local varlist `r(varlist)'
}

if `"`generate'"'!="" _assert `:word count `generate''==`:word count `varlist'', msg("generate() option has wrong number of variable names") rc(198)

if `"`exp'"' != "" {
cap confirm var `exp'
if _rc {
tempname wtvar
gen double `wtvar' = `exp' if `touse'
}
else local wtvar `exp'
local wtopt , weights = :`wtvar'
}

local blaslib = cond(c(os)=="MacOSX", "AppleAccelerate", "BLISBLAS")
local gpulib = cond(c(os)=="MacOSX", "Metal", "CUDA")

jl AddPkg `blaslib'
jl AddPkg `gpulib'
jl AddPkg FixedEffectModels, minver(1.10.2)
jl, qui: using `blaslib', `gpulib', FixedEffectModels

tokenize `absorb'
local absorb `*' // remove extra spaces
local feterms i.`: subinstr local absorb " " " + i.", all'

local absorbvars `feterms'
local feterms: subinstr local feterms "##c." ")*(", all
local feterms: subinstr local feterms "#c." ")&(", all
local feterms: subinstr local feterms "##i." ")*fe(", all
local feterms: subinstr local feterms "##" "#", all
local feterms: subinstr local feterms "#" "#i.", all
local feterms: subinstr local feterms "i.i." "i.", all
local feterms: subinstr local feterms "#i." ")&fe(", all
local feterms: subinstr local feterms "i." "fe(", all
local feterms: subinstr local feterms " " ") + ", all
local feterms: subinstr local feterms ")" " )", all
local feterms: subinstr local feterms "(" "( ", all
local feterms + `feterms' )

local absorbvars: subinstr local absorbvars "i." " ", all
local absorbvars: subinstr local absorbvars "c." " ", all
local absorbvars: subinstr local absorbvars "#" " ", all

local absorbvars `absorbvars'
foreach var in `absorbvars' {
cap confirm numeric var `var'
if _rc {
tempvar t
egen long `t' = group(`var') if `touse'
local absorbvars: subinstr local absorbvars "`var'" "`t'", word all
local feterms : subinstr local feterms "`var'" "`t'", word all
}
}

markout `touse' `absorbvars'

jl PutVarsToDFNoMissing `varlist' `absorbvars' `wtvar' if `touse'
jl, qui: p = partial_out(df, @formula(`:subinstr local varlist " " " + ", all' ~ 1 `feterms') `wtopt', add_mean=false)

if `"`prefix'"' != "" local generate `prefix'`: subinstr local varlist " " " `prefix'", all'
else if "`replace'"!="" {
tempname t
jl, qui: SF_scal_save("`t'", size(df)[1])
if `t' < _N {
foreach var in `generate' {
cap replace `var' = .
}
}
}

jl GetVarsFromDF `generate' if `touse', source(p[1]) cols(`varlist') `replace'

jl: df = p = nothing
end
28 changes: 13 additions & 15 deletions reghdfejl.ado
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,7 @@ program define reghdfejl, eclass
exit 0
}

jl version
if "`r(version)'" != "`JLVERSION'" {
di as txt "The Stata package julia is not up to date. Attempting to update it with {stata ssc install julia, replace}." _n
ssc install julia, replace
jl version
_assert "`r(version)'"=="`JLVERSION'", msg(Failed to install required version of the julia package.) rc(198)
}

syntax anything [if] [in] [aw pw iw/], [Absorb(string) Robust CLuster(string) vce(string) RESIDuals ITerations(integer 16000) gpu THReads(integer 0) ///
syntax anything [if] [in] [aw pw iw fw/], [Absorb(string) Robust CLuster(string) vce(string) RESIDuals ITerations(integer 16000) gpu THReads(integer 0) ///
noSAMPle TOLerance(real 1e-8) Level(real `c(level)') NOHEADer NOTABLE compact *]
local sample = "`sample'"==""

Expand All @@ -114,19 +106,24 @@ program define reghdfejl, eclass
if `threads' local threadsopt , nthreads = `threads'

if `"$reghdfejl_loaded"'=="" {
cap jl: nothing
cap jl version
if _rc {
di as err `"Can't access Julia. {cmd:reghdfejl} requires that the {cmd:jl} command be installed, via {stata ssc install julia}."
di as err "And it requires that Julia be installed, following the instruction under Installation in {help jl##installation:help jl}."
exit 198
}
if "`r(version)'" != "`JLVERSION'" {
di as txt "The Stata package {cmd:julia} is not up to date. Attempting to update it with {stata ssc install julia, replace}." _n
ssc install julia, replace
}
local blaslib = cond(c(os)=="MacOSX", "AppleAccelerate", "BLISBLAS")
jl AddPkg `blaslib'
jl AddPkg `gpulib'
jl AddPkg FixedEffectModels, minver(1.10.2)
jl AddPkg DataFrames, minver(1.6.1)
jl AddPkg Vcov, minver(0.8.1)
jl, qui: using `blaslib', `gpulib', FixedEffectModels, DataFrames, Vcov
jl, qui: using `blaslib', `gpulib', FixedEffectModels, Vcov
global reghdfejl_loaded 1
}
Expand Down Expand Up @@ -337,9 +334,10 @@ program define reghdfejl, eclass
jl, qui: esample = nothing
}
tempname b V
jl, qui: SF_scal_save("`b'", length(coef(m)))
if `b' {
jl, qui: SF_scal_save("`t'", length(coef(m)))
if `t' {
tempname b V
jl, qui: SF_scal_save("`b'", coefnames(m)[1]=="(Intercept)")
local hascons = `b'
Expand Down
3 changes: 3 additions & 0 deletions reghdfejl.sthlp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ displays these corrections in a table after the main results. {cmd:reghdfejl} do
{p 4 6 0}
* It does not offer the {help reghdfe##opt_group_fes:Group FE} features.

{p 4 6 0}
* It does not accept frequency weights ("fweights").

{p 4 6 0}
* It does not allow control over whether the constant term is reported. The constant is always absorbed.

Expand Down

0 comments on commit b4ea020

Please sign in to comment.