Commit d88596f6 authored by Ole Voldsæter's avatar Ole Voldsæter

Merge branch 'develop' into 21-diff-it

parents 27876578 16cc80ad
......@@ -446,6 +446,7 @@ parenthesized
number
: #"-?\d+(\.\d+)?"
| #"-?\.\d+"
;
function_invocation
......
......@@ -155,10 +155,17 @@
)
:varname_operand (fn [& args] {:variableName (first args), "$type" "VariableSymbolExpression"})
:parenthesized (fn [& args] (first args))
:number (fn [& args] (read-string (first args)))
:number (fn [& [numlit]]
(if (starts-with? numlit ".")
(Double/parseDouble numlit)
(read-string numlit)
)
)
:function_invocation function-lookup
:string (fn [& args] (apply str (rest (butlast (first args)))))
:missing (fn [& args] {:missing (first args)})
:missing (fn [& [value]]
{"$type" "MissingValueConstantExpression"
:value value})
:recode (fn [& [vars rules filter options]]
(let [varlist (recode-varlist (expand-varlist vars *variables*) (or options {}))]
{:command "recode"
......
......@@ -10,7 +10,9 @@
; anymatch
; anyvalue
; concat
; cut
; ends
; group
(defonce compute-funcs {
:diff "diff"
......@@ -29,14 +31,6 @@
:tag "tag"
})
; --------------- aggregate? ------------
; mode
; pc
; rank
; total
; seq
(defonce aggregate-funcs {
:count "agg_count"
:iqr "agg_iqr"
......@@ -63,17 +57,24 @@
(defn make-var-struct [varname]
{:variableName varname , "$type" "VariableSymbolExpression"})
(defn make-compute [varname funcname expr filter]
(defn make-compute [varname funcname args filter]
{:command "compute"
:variable varname
:expression {:function funcname
:isSdtlName true
:arguments expr
:arguments args
"$type" "FunctionCallExpression"}
:condition filter
:mod-varlist (vec (conj *variables* varname))}
)
(defn make-aggregate [by varname funcname expr filter]
{:command "aggregate"
:groupByVariables by
:aggregateVariables [(dissoc (make-compute varname funcname expr filter) :mod-varlist)]
:mod-varlist (vec (conj *variables* varname))}
)
(defn varlist-compute [varname funcname varlist filter options]
(case funcname
:rowpctile (make-compute
......@@ -110,8 +111,10 @@
)
(defn varlist-aggregate [by-clause varname funcname varlist filter options]
;; ------ schema for this doesn't exist yet ---------
(unsupported-func funcname)
(case funcname
:mode (unsupported-func funcname)
(make-aggregate by-clause varname (get aggregate-funcs (keyword funcname)) (map make-var-struct (expand-varlist varlist *variables*)) filter)
)
)
(defn egen-varlist [& [by-clause varname funcname varlist filter options]]
......@@ -145,7 +148,7 @@
(case funcname
:std (make-compute
varname
"std"
"std_val"
(let [m (get-var-length-option :mean 1 options)
s (get-var-length-option :std 1 options)
m (if m (get options m) [[0]])
......@@ -153,8 +156,14 @@
[(-> m first first) (-> s first first) expr]
)
filter)
;;;----- all other egen/expr functions are aggregators -------------
(unsupported-func funcname)
:pctile (make-aggregate by-clause varname (get aggregate-funcs (keyword funcname)) [expr (-> (:p options [[50]]) first first)] filter)
;;;----- weird ones -------------
:rank (unsupported-func funcname)
:pc (unsupported-func funcname)
;;; default
(make-aggregate by-clause varname (get aggregate-funcs (keyword funcname)) [expr] filter)
)
)
)
\ No newline at end of file
(ns stata2sdtl.egen-test
(:require [clojure.test :refer :all]
[instaparse.transform :refer [transform]]
[stata2sdtl.egen :refer :all]))
(deftest make-compute-test
(is
(= (make-compute "some-var" "min" [5] 5)
'{:command "compute"
:condition 5
:expression {"$type" "FunctionCallExpression"
:arguments [5]
:function "min"
:isSdtlName true}
:mod-varlist ["some-var"]
:variable "some-var"})
)
)
(deftest make-aggregate-test
(is
(= (make-aggregate ["a" "b"] "some-var" :mean 5 5)
'{:aggregateVariables [{:command "compute"
:condition 5
:expression {"$type" "FunctionCallExpression"
:arguments 5
:function :mean
:isSdtlName true}
:variable "some-var"}]
:command "aggregate"
:groupByVariables ["a"
"b"]
:mod-varlist ["some-var"]})
)
)
(deftest varlist-compute-test
(is
(= (varlist-compute "some-var" :rowpctile ["a" "b"] nil nil)
'{:command "compute"
:condition nil
:expression {"$type" "FunctionCallExpression"
:arguments [50
{"$type" "VariableSymbolExpression"
:variableName "a"}
{"$type" "VariableSymbolExpression"
:variableName "b"}]
:function "rowpctile"
:isSdtlName true}
:mod-varlist ["some-var"]
:variable "some-var"})
)
(is
(= (varlist-compute "some-var" :rowpctile ["a" "b"] 5 {:p [[10]]})
'{:command "compute"
:condition 5
:expression {"$type" "FunctionCallExpression"
:arguments [10
{"$type" "VariableSymbolExpression"
:variableName "a"}
{"$type" "VariableSymbolExpression"
:variableName "b"}]
:function "rowpctile"
:isSdtlName true}
:mod-varlist ["some-var"]
:variable "some-var"})
)
(is
(= (varlist-compute "some-var" :rowtotal ["a" "b"] nil nil)
'{:command "compute"
:condition nil
:expression {"$type" "FunctionCallExpression"
:arguments [false
{"$type" "VariableSymbolExpression"
:variableName "a"}
{"$type" "VariableSymbolExpression"
:variableName "b"}]
:function "rowtotal"
:isSdtlName true}
:mod-varlist ["some-var"]
:variable "some-var"})
)
(is
(= (varlist-compute "some-var" :rowtotal ["a" "b"] 5 {:missing nil})
'{:command "compute"
:condition 5
:expression {"$type" "FunctionCallExpression"
:arguments [true
{"$type" "VariableSymbolExpression"
:variableName "a"}
{"$type" "VariableSymbolExpression"
:variableName "b"}]
:function "rowtotal"
:isSdtlName true}
:mod-varlist ["some-var"]
:variable "some-var"})
)
(is
(= (varlist-compute "some-var" :tag ["a" "b"] nil nil)
'{:command "compute"
:condition nil
:expression {"$type" "FunctionCallExpression"
:arguments [false
{"$type" "VariableSymbolExpression"
:variableName "a"}
{"$type" "VariableSymbolExpression"
:variableName "b"}]
:function "tag"
:isSdtlName true}
:mod-varlist ["some-var"]
:variable "some-var"})
)
(is
(= (varlist-compute "some-var" :tag ["a" "b"] 5 {:missing nil})
'{:command "compute"
:condition 5
:expression {"$type" "FunctionCallExpression"
:arguments [true
{"$type" "VariableSymbolExpression"
:variableName "a"}
{"$type" "VariableSymbolExpression"
:variableName "b"}]
:function "tag"
:isSdtlName true}
:mod-varlist ["some-var"]
:variable "some-var"})
)
(is
(= (varlist-compute "some-var" "diff" ["a" "b"] 5 nil)
'{:command "compute"
:condition 5
:expression {"$type" "FunctionCallExpression"
:arguments ({"$type" "VariableSymbolExpression"
:variableName "a"}
{"$type" "VariableSymbolExpression"
:variableName "b"})
:function "diff"
:isSdtlName true}
:mod-varlist ["some-var"]
:variable "some-var"})
)
)
(deftest egen-expr-test
(is
(= (egen-expr nil "somevar" "std" 5 1 nil)
'{:command "compute"
:condition 1
:expression {"$type" "FunctionCallExpression"
:arguments [0
1
5]
:function "std_val"
:isSdtlName true}
:mod-varlist ["somevar"]
:variable "somevar"}
)
)
(is
(= (egen-expr nil "somevar" "std" 5 1 {:mean [[7]]})
'{:command "compute"
:condition 1
:expression {"$type" "FunctionCallExpression"
:arguments [7
1
5]
:function "std_val"
:isSdtlName true}
:mod-varlist ["somevar"]
:variable "somevar"}
)
)
(is
(= (egen-expr nil "somevar" "std" 5 1 {:std [[2]] :mean [[7]]})
'{:command "compute"
:condition 1
:expression {"$type" "FunctionCallExpression"
:arguments [7
2
5]
:function "std_val"
:isSdtlName true}
:mod-varlist ["somevar"]
:variable "somevar"}
)
)
(is
(= (egen-expr ["a" "b"] "somevar" "pctile" 5 1 nil)
'{:aggregateVariables [{:command "compute"
:condition 1
:expression {"$type" "FunctionCallExpression"
:arguments [5
50]
:function "agg_pctile"
:isSdtlName true}
:variable "somevar"}]
:command "aggregate"
:groupByVariables ["a"
"b"]
:mod-varlist ["somevar"]}
)
)
(is
(= (egen-expr ["a" "b"] "somevar" "pctile" 5 1 {:p [[10]]})
'{:aggregateVariables [{:command "compute"
:condition 1
:expression {"$type" "FunctionCallExpression"
:arguments [5
10]
:function "agg_pctile"
:isSdtlName true}
:variable "somevar"}]
:command "aggregate"
:groupByVariables ["a"
"b"]
:mod-varlist ["somevar"]}
)
)
)
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment