@@ -66,67 +66,74 @@ function result(::Type{Reg}, @nospecialize(nt::NamedTuple))
6666 return merge (nt, (result= didresult,))
6767end
6868
69+ """
70+ has_fe(r::RegressionBasedDIDResult)
71+
72+ Test whether any fixed effect is involved in regression.
73+ """
6974has_fe (r:: RegressionBasedDIDResult ) = ! isempty (r. fenames)
7075
71- format_scientific (f:: Number ) = @sprintf (" %.3f" , f)
72-
73- function _summary (r:: RegressionBasedDIDResult )
74- top = [" Number of obs" sprint (show, nobs (r), context= :compact => true );
75- " Degrees of freedom" sprint (show, nobs (r) - dof_residual (r), context= :compact => true );
76- " F-statistic" sprint (show, r. F, context = :compact => true );
77- " p-value" format_scientific (r. p)]
78- fe_info = has_fe (r) ?
79- [" Converged" sprint (show, r. feconverged, context= :compact => true );
80- " Singletons dropped" sprint (show, r. nfesingledropped, context= :compact => true );
81- ] : nothing
82- return top, fe_info
83- end
76+ _top_info (r:: RegressionBasedDIDResult ) = (
77+ " Number of obs" => nobs (r),
78+ " Degrees of freedom" => nobs (r) - dof_residual (r),
79+ " F-statistic" => TestStat (r. F),
80+ " p-value" => PValue (r. p)
81+ )
8482
8583_nunique (t:: Table , s:: Symbol ) = length (unique (getproperty (t, s)))
8684
87- _excluded_rel_str (tr:: DynamicTreatment ) =
88- isempty (tr. exc) ? " None" : join (string .(tr. exc), " " )
85+ _excluded_rel_str (tr:: DynamicTreatment ) =
86+ isempty (tr. exc) ? " none" : join (string .(tr. exc), " " )
87+
88+ _treat_info (r:: RegressionBasedDIDResult{true} , tr:: DynamicTreatment ) = (
89+ " Number of cohorts" => _nunique (r. treatinds, r. treatname),
90+ " Interactions within cohorts" => length (columnnames (r. treatinds)) - 2 ,
91+ " Relative time periods" => _nunique (r. treatinds, :rel ),
92+ " Excluded periods" => NoQuote (_excluded_rel_str (tr))
93+ )
8994
90- _treat_info (tr:: DynamicTreatment , trinds:: Table , treatname:: Symbol ) =
91- [" Number of cohorts" sprint (show, _nunique (trinds, treatname), context= :compact => true );
92- " Interactions within cohorts" sprint (show, length (columnnames (trinds))- 2 , context= :compact => true );
93- " Relative time periods" sprint (show, _nunique (trinds, :rel ), context= :compact => true );
94- " Excluded periods" sprint (show, NoQuote (_excluded_rel_str (tr)), context= :compact => true )]
95+ _treat_info (r:: RegressionBasedDIDResult{false} , tr:: DynamicTreatment ) = (
96+ " Relative time periods" => _nunique (r. treatinds, :rel ),
97+ " Excluded periods" => NoQuote (_excluded_rel_str (tr))
98+ )
9599
96100_treat_spec (r:: RegressionBasedDIDResult{true} , tr:: DynamicTreatment{SharpDesign} ) =
97101 " Cohort-interacted sharp dynamic specification"
98102
99103_treat_spec (r:: RegressionBasedDIDResult{false} , tr:: DynamicTreatment{SharpDesign} ) =
100104 " Sharp dynamic specification"
101-
102- function show (io:: IO , r:: RegressionBasedDIDResult ;
103- totalwidth:: Int = 70 , interwidth:: Int = 4 + mod (totalwidth,2 ))
105+
106+ _fe_info (r:: RegressionBasedDIDResult ) = (
107+ " Converged" => r. feconverged,
108+ " Singletons dropped" => r. nfesingledropped
109+ )
110+
111+ show (io:: IO , :: RegressionBasedDIDResult ) = print (io, " Regression-based DID result" )
112+
113+ function show (io:: IO , :: MIME"text/plain" , r:: RegressionBasedDIDResult ;
114+ totalwidth:: Int = 70 , interwidth:: Int = 4 + mod (totalwidth,2 ))
104115 halfwidth = div (totalwidth- interwidth, 2 )
105- top, fe_info = _summary (r)
106- tr_info = _treat_info (r. tr, r. treatinds, r. treatname)
107- blocks = [top, tr_info, fe_info]
116+ top_info = _top_info (r)
117+ fe_info = has_fe (r) ? _fe_info (r) : ()
118+ tr_info = _treat_info (r, r. tr)
119+ blocks = (top_info, tr_info, fe_info)
108120 fes = has_fe (r) ? join (string .(r. fenames), " " ) : " none"
109121 fetitle = string (" Fixed effects: " , fes)
110- blocktitles = [ " Summary of results" ,
111- _treat_spec (r, r. tr),
112- fetitle[ 1 : min (totalwidth, length (fetitle))]]
122+ blocktitles = ( " Summary of results: Regression-based DID " ,
123+ _treat_spec (r, r. tr), fetitle[ 1 : min (totalwidth, length (fetitle))])
124+
113125 for (ib, b) in enumerate (blocks)
114- for i in 1 : size (b, 1 )
115- b[i, 1 ] = b[i, 1 ] * " :"
116- end
117- println (io, " ─" ^ totalwidth)
126+ println (io, repeat (' ─' , totalwidth))
118127 println (io, blocktitles[ib])
119- println (io, " ─" ^ totalwidth)
120- for i in 1 : (div (size (b, 1 ) - 1 , 2 )+ 1 )
121- print (io, b[2 * i- 1 , 1 ])
122- print (io, lpad (b[2 * i- 1 , 2 ], halfwidth - length (b[2 * i- 1 , 1 ])))
123- print (io, " " ^ interwidth)
124- if size (b, 1 ) >= 2 * i
125- print (io, b[2 * i, 1 ])
126- print (io, lpad (b[2 * i, 2 ], halfwidth - length (b[2 * i, 1 ])))
128+ if ! isempty (b)
129+ # Print line between block title and block content
130+ println (io, repeat (' ─' , totalwidth))
131+ for (i, e) in enumerate (b)
132+ print (io, e[1 ], ' :' )
133+ print (io, lpad (e[2 ], halfwidth - length (e[1 ]) - 1 ))
134+ print (io, isodd (i) ? repeat (' ' , interwidth) : ' \n ' )
127135 end
128- println (io)
129136 end
130137 end
131- println (io, " ─ " ^ totalwidth)
138+ print (io, repeat ( ' ─ ' , totalwidth) )
132139end
0 commit comments