@@ -22,7 +22,7 @@ function parse_interactivefixedeffect(df::AbstractDataFrame, formula::FormulaTer
22
22
m = nothing
23
23
for term in FixedEffectModels. eachterm (formula. rhs)
24
24
if term isa FunctionTerm{typeof (ife)}
25
- m = InteractiveFixedEffectTerm (term. args_parsed [1 ]. sym, term. args_parsed [2 ]. sym, term. args_parsed [3 ]. n)
25
+ m = InteractiveFixedEffectTerm (term. args [1 ]. sym, term. args [2 ]. sym, term. args [3 ]. n)
26
26
elseif term isa InteractiveFixedEffectTerm
27
27
m = term
28
28
end
@@ -255,30 +255,38 @@ struct InteractiveFixedEffectModel <: RegressionModel
255
255
augmentdf:: DataFrame
256
256
257
257
coefnames:: Vector # Name of coefficients
258
- yname:: Symbol # Name of dependent variable
259
- formula:: FormulaTerm # Original formula
258
+ responsename:: Symbol # Name of dependent variable
259
+ formula:: FormulaTerm # Original formula
260
+ formula_schema:: FormulaTerm # Schema for predict
260
261
261
262
nobs:: Int64 # Number of observations
262
263
dof_residual:: Int64 # degree of freedoms
263
264
265
+ rss:: Float64
266
+ tss:: Float64
264
267
r2:: Float64 # R squared
265
268
adjr2:: Float64 # R squared adjusted
266
269
r2_within:: Float64 # R within
267
270
268
- rss:: Float64
269
271
iterations:: Int # Number of iterations
270
272
converged:: Bool # Has the demeaning algorithm converged?
271
273
272
274
end
273
- StatsBase. coef (x:: InteractiveFixedEffectModel ) = x. coef
274
- StatsBase. coefnames (x:: InteractiveFixedEffectModel ) = x. coefnames
275
- StatsBase. vcov (x:: InteractiveFixedEffectModel ) = x. vcov
276
- StatsBase. nobs (x:: InteractiveFixedEffectModel ) = x. nobs
277
- StatsBase. dof_residual (x:: InteractiveFixedEffectModel ) = x. dof_residual
278
- StatsBase. r2 (x:: InteractiveFixedEffectModel ) = x. r2
279
- StatsBase. adjr2 (x:: InteractiveFixedEffectModel ) = x. adjr2
280
- StatsBase. islinear (x:: InteractiveFixedEffectModel ) = false
281
- StatsBase. rss (x:: InteractiveFixedEffectModel ) = x. rss
275
+ StatsAPI. coef (x:: InteractiveFixedEffectModel ) = x. coef
276
+ StatsAPI. coefnames (x:: InteractiveFixedEffectModel ) = x. coefnames
277
+ StatsAPI. responsename (m:: InteractiveFixedEffectModel ) = m. responsename
278
+ StatsAPI. vcov (x:: InteractiveFixedEffectModel ) = x. vcov
279
+ StatsAPI. nobs (x:: InteractiveFixedEffectModel ) = x. nobs
280
+ StatsAPI. dof_residual (x:: InteractiveFixedEffectModel ) = x. dof_residual
281
+ StatsAPI. r2 (x:: InteractiveFixedEffectModel ) = x. r2
282
+ StatsAPI. adjr2 (x:: InteractiveFixedEffectModel ) = x. adjr2
283
+ StatsAPI. islinear (x:: InteractiveFixedEffectModel ) = false
284
+ StatsAPI. deviance (x:: InteractiveFixedEffectModel ) = x. tss
285
+ StatsAPI. rss (x:: InteractiveFixedEffectModel ) = x. rss
286
+ StatsAPI. mss (m:: InteractiveFixedEffectModel ) = deviance (m) - rss (m)
287
+ StatsModels. formula (m:: InteractiveFixedEffectModel ) = m. formula_schema
288
+
289
+
282
290
StatsBase. predict (:: InteractiveFixedEffectModel , :: AbstractDataFrame ) = error (" predict is not defined for linear factor models. Use the option save = true" )
283
291
StatsBase. residuals (:: InteractiveFixedEffectModel , :: AbstractDataFrame ) = error (" residuals is not defined for linear factor models. Use the option save = true" )
284
292
function StatsBase. confint (x:: InteractiveFixedEffectModel ; level:: Real = 0.95 )
@@ -316,97 +324,69 @@ title(::InteractiveFixedEffectModel) = "Interactive Fixed Effect Model"
316
324
top (x:: InteractiveFixedEffectModel ) = [
317
325
" Number of obs" sprint (show, nobs (x); context= :compact => true );
318
326
" Degree of freedom" sprint (show, nobs (x) - dof_residual (x); context= :compact => true );
319
- " R2 " @sprintf (" %.3f" , x. r2);
320
- " R2 within" @sprintf (" %.3f" , x. r2_within);
327
+ " R² " @sprintf (" %.3f" , x. r2);
328
+ " R² within" @sprintf (" %.3f" , x. r2_within);
321
329
" Iterations" sprint (show, x. iterations; context= :compact => true );
322
330
" Converged" sprint (show, x. converged; context= :compact => true )
323
331
]
324
332
325
- format_scientific (x) = @sprintf (" %.3f" , x)
326
-
327
- function Base. show (io:: IO , x:: InteractiveFixedEffectModel )
328
- ctitle = title (x)
329
- ctop = top (x)
330
- cc = coef (x)
331
- se = stderror (x)
332
- coefnms = coefnames (x)
333
- conf_int = confint (x)
334
- # put (intercept) last
335
- if ! isempty (coefnms) && ((coefnms[1 ] == Symbol (" (Intercept)" )) || (coefnms[1 ] == " (Intercept)" ))
336
- newindex = vcat (2 : length (cc), 1 )
337
- cc = cc[newindex]
338
- se = se[newindex]
339
- conf_int = conf_int[newindex, :]
340
- coefnms = coefnms[newindex]
341
- end
342
- tt = cc ./ se
343
- mat = hcat (cc, se, tt, fdistccdf .(Ref (1 ), Ref (dof_residual (x)), abs2 .(tt)), conf_int[:, 1 : 2 ])
344
- nr, nc = size (mat)
345
- colnms = [" Estimate" ," Std.Error" ," t value" , " Pr(>|t|)" , " Lower 95%" , " Upper 95%" ]
346
- rownms = [" $(coefnms[i]) " for i = 1 : length (cc)]
347
- pvc = 4
348
-
349
-
350
- # print
333
+ import StatsBase: NoQuote, PValue
334
+ function Base. show (io:: IO , m:: InteractiveFixedEffectModel )
335
+ ct = coeftable (m)
336
+ # copied from show(iio,cf::Coeftable)
337
+ cols = ct. cols; rownms = ct. rownms; colnms = ct. colnms;
338
+ nc = length (cols)
339
+ nr = length (cols[1 ])
351
340
if length (rownms) == 0
352
- rownms = AbstractString[lpad (" [$i ]" ,floor (Integer, log10 (nr))+ 3 ) for i in 1 : nr]
353
- end
354
- if length (rownms) > 0
355
- rnwidth = max (4 ,maximum ([length (nm) for nm in rownms]) + 1 )
356
- else
357
- # if only intercept, rownms is empty collection, so previous would return error
358
- rnwidth = 4
341
+ rownms = [lpad (" [$i ]" ,floor (Integer, log10 (nr))+ 3 ) for i in 1 : nr]
359
342
end
360
- rownms = [rpad (nm,rnwidth) for nm in rownms]
361
- widths = [length (cn):: Int for cn in colnms]
362
- str = [sprint (show, mat[i,j]; context= :compact => true ) for i in 1 : nr, j in 1 : nc]
363
- if pvc != 0 # format the p-values column
364
- for i in 1 : nr
365
- str[i, pvc] = format_scientific (mat[i, pvc])
366
- end
343
+ mat = [j == 1 ? NoQuote (rownms[i]) :
344
+ j- 1 == ct. pvalcol ? NoQuote (sprint (show, PValue (cols[j- 1 ][i]))) :
345
+ j- 1 in ct. teststatcol ? TestStat (cols[j- 1 ][i]) :
346
+ cols[j- 1 ][i] isa AbstractString ? NoQuote (cols[j- 1 ][i]) : cols[j- 1 ][i]
347
+ for i in 1 : nr, j in 1 : nc+ 1 ]
348
+ io = IOContext (io, :compact => true , :limit => false )
349
+ A = Base. alignment (io, mat, 1 : size (mat, 1 ), 1 : size (mat, 2 ),
350
+ typemax (Int), typemax (Int), 3 )
351
+ nmswidths = pushfirst! (length .(colnms), 0 )
352
+ A = [nmswidths[i] > sum (A[i]) ? (A[i][1 ]+ nmswidths[i]- sum (A[i]), A[i][2 ]) : A[i]
353
+ for i in 1 : length (A)]
354
+ totwidth = sum (sum .(A)) + 2 * (length (A) - 1 )
355
+
356
+
357
+ # intert my stuff which requires totwidth
358
+ ctitle = string (typeof (m))
359
+ halfwidth = div (totwidth - length (ctitle), 2 )
360
+ print (io, " " ^ halfwidth * ctitle * " " ^ halfwidth)
361
+ ctop = top (m)
362
+ for i in 1 : size (ctop, 1 )
363
+ ctop[i, 1 ] = ctop[i, 1 ] * " :"
367
364
end
368
- for j in 1 : nc
369
- for i in 1 : nr
370
- lij = length (str[i, j])
371
- if lij > widths[j]
372
- widths[j] = lij
373
- end
365
+ println (io, ' \n ' , repeat (' =' , totwidth))
366
+ halfwidth = div (totwidth, 2 ) - 1
367
+ interwidth = 2 + mod (totwidth, 2 )
368
+ for i in 1 : (div (size (ctop, 1 ) - 1 , 2 )+ 1 )
369
+ print (io, ctop[2 * i- 1 , 1 ])
370
+ print (io, lpad (ctop[2 * i- 1 , 2 ], halfwidth - length (ctop[2 * i- 1 , 1 ])))
371
+ print (io, " " ^ interwidth)
372
+ if size (ctop, 1 ) >= 2 * i
373
+ print (io, ctop[2 * i, 1 ])
374
+ print (io, lpad (ctop[2 * i, 2 ], halfwidth - length (ctop[2 * i, 1 ])))
374
375
end
376
+ println (io)
375
377
end
376
- widths .+ = 1
377
- totalwidth = sum (widths) + rnwidth
378
- if length (ctitle) > 0
379
- halfwidth = div (totalwidth - length (ctitle), 2 )
380
- println (io, " " ^ halfwidth * string (ctitle) * " " ^ halfwidth)
378
+
379
+ # rest of coeftable code
380
+ println (io, repeat (' =' , totwidth))
381
+ print (io, repeat (' ' , sum (A[1 ])))
382
+ for j in 1 : length (colnms)
383
+ print (io, " " , lpad (colnms[j], sum (A[j+ 1 ])))
381
384
end
382
- if length (ctop) > 0
383
- for i in 1 : size (ctop, 1 )
384
- ctop[i, 1 ] = ctop[i, 1 ] * " :"
385
- end
386
- println (io, " =" ^ totalwidth)
387
- halfwidth = div (totalwidth, 2 ) - 1
388
- interwidth = 2 + mod (totalwidth, 2 )
389
- for i in 1 : (div (size (ctop, 1 ) - 1 , 2 )+ 1 )
390
- print (io, ctop[2 * i- 1 , 1 ])
391
- print (io, lpad (ctop[2 * i- 1 , 2 ], halfwidth - length (ctop[2 * i- 1 , 1 ])))
392
- print (io, " " ^ interwidth)
393
- if size (ctop, 1 ) >= 2 * i
394
- print (io, ctop[2 * i, 1 ])
395
- print (io, lpad (ctop[2 * i, 2 ], halfwidth - length (ctop[2 * i, 1 ])))
396
- end
397
- println (io)
398
- end
385
+ println (io, ' \n ' , repeat (' ─' , totwidth))
386
+ for i in 1 : size (mat, 1 )
387
+ Base. print_matrix_row (io, mat, A, i, 1 : size (mat, 2 ), " " )
388
+ i != size (mat, 1 ) && println (io)
399
389
end
400
- println (io," =" ^ totalwidth)
401
- println (io," " ^ rnwidth *
402
- join ([lpad (string (colnms[i]), widths[i]) for i = 1 : nc], " " ))
403
- println (io," -" ^ totalwidth)
404
- for i in 1 : nr
405
- print (io, rownms[i])
406
- for j in 1 : nc
407
- print (io, lpad (str[i,j],widths[j]))
408
- end
409
- println (io)
410
- end
411
- println (io," =" ^ totalwidth)
412
- end
390
+ println (io, ' \n ' , repeat (' =' , totwidth))
391
+ nothing
392
+ end
0 commit comments