Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove term from formula #318

Open
kliegl opened this issue Oct 15, 2024 · 1 comment
Open

Remove term from formula #318

kliegl opened this issue Oct 15, 2024 · 1 comment

Comments

@kliegl
Copy link

kliegl commented Oct 15, 2024

In a A x B design, how do I remove one main effect and keep the other and the interaction term (e.g., remove Group main effect in Group (2) x Time (2) when I do not expect a group difference at pretest)?

I can add terms to a formula:

f1 = @formula y ~ 1 + Time  +  (1  |  Subj);  
f1 = f1.lhs ~ (f1.rhs + Term(:Time & :Group) )
m1 = fit(MixedModel, f1, dat; contrasts)

But how can I substract a term? For example, could this be implemented, too?

f2 = @formula y ~ 1 + Time + Group + Time & Group +  (1  | Subj);  
f2 = f2.lhs ~ (f2.rhs - Term(:Group) )

ERROR: MethodError: no method matching -(::Tuple{ConstantTerm{Int64}, Term, InteractionTerm{Tuple{…}}, FunctionTerm{typeof(|), Vector{…}}}, ::Term)
The function - exists, but no method is defined for this combination of argument types.

One workaround is to extract terms from a fitted model matrix and then assemble the needed ones to a new formula:

f3 = @formula y ~ 1 + Group*Time  +  (1  |  Subj);  
m3 = fit(MixedModel, f3, dat; contrasts)
mm3 = modelmatrix(m3)
dat.T    = mm3[:, 3];
dat.TxG  = mm3[:, 4];
f4 = @formula y ~ 1 + T  +  TxG + (1  | Subj); 
m4 = fit(MixedModel, f4, dat; contrasts)

However, this becomes very cumbersome with many terms nested under the first one. Or is there a simple loop to assemble a formula selectively from the model matrix? Adding a subtraction method would be very helpful. Thank you.

@kliegl kliegl changed the title Remove Remove term from formula Oct 15, 2024
@kliegl
Copy link
Author

kliegl commented Oct 15, 2024

The problem applies only to categorical variables in the formula; excluding terms from the formula works as expected when categorical variables are replaced with (sets of) indicator variables. No need for the loop. I leave the issue open for now because the subtraction method would be nice to have anyway. Feel free to close the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant