Skip to content

Interaction #76

Description

@matthieugomez

At some point do better path if there is fe + fe * interaction. I think one simple way is to demean interaction variable within each fixed effect, cause one can see that the resuling intercept is of Y on intercept and interaction within each group is simply given by E[Y] + E[YX] / E[X^2] * X

Activity

  1. matthieugomez commented on Feb 3, 2026

    @matthieugomez
    MemberAuthor

    Something like

    function demean_interaction!(feM::FixedEffectSolverCPU{T}) where T
    	m = feM.m
    	fes = m.fes
    	weights = feM.weights
    	outs = Any[]
    	for (i, fe) in enumerate(fes)
    		# if fe is interacted fixed effects
    		if !isa(fe.interaction, UnitWeights)
    			# find if there is the non interacted version somewhere
    			for fe2 in fes
    				if isa(fe2.interaction, UnitWeights) && (fe.n == fe2.n) && (fe.refs == fe2.refs)
    					# if so, we can demean interaction. Will speed up process.
    					interaction_demeaned = deepcopy(fe.interaction)
    					out = zeros(T, fe.n)
    					gather!(out, fe.refs, 1, interaction_demeaned, weights)
    					out2 = zeros(T, fe.n)
    					gather!(out2, fe.refs, 1, weights, uweights(length(weights)))
    					out .= out ./ out2
    					scatter!(interaction_demeaned, -1, out, fe.refs, uweights(length(weights)))
    					fes[i] = FixedEffect{typeof(fe.refs), typeof(fe.interaction)}(fe.refs, interaction_demeaned, fe.n)
    					scale!(m.scales[i], fe.refs, fe.interaction, weights)
    					cache!(m.caches[i], fe.refs, fe.interaction, weights, m.scales[i])
    					push!(outs, [i, out])
    					break
    				end
    			end
    		end
    	end
    	return outs
    end
    
    function remean_interaction!(feM::FixedEffectSolverCPU{T}, outs) where {T}
    	m = feM.m
    	fes = m.fes
    	weights = feM.weights
    	for (i, out) in outs
    		fe = fes[i]
    		interaction_demeaned = deepcopy(fe.interaction)
    		scatter!(interaction_demeaned, 1, out, fe.refs, uweights(length(weights)))
    		fes[i] = FixedEffect{typeof(fe.refs), typeof(fe.interaction)}(fe.refs, interaction_demeaned, fe.n)
    		scale!(m.scales[i], fe.refs, interaction_demeaned, weights)
    		cache!(m.caches[i], fe.refs, interaction_demeaned, weights, m.scales[i])
    	end
    end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions