Search code examples
rglm

How to fit a slope modifier in glm (R)


I would like to fit a general linear model of the form:

lp = c + (a1+a2*Test)*x

Where c is the intercept (to be fitted), a1 is the coefficient for x, and a2 is the modifier to the coefficient of x for the second level of a 2-level factor 'Test' (here coded as a dummy variable value '1').

So for the first level of the factor 'Test', value 0 of the dummy variable, the slope is a1. For the second level of 'Test', the slope is (a1+a2).

In GenStat, I could fit this as 'x+Test.x' in the Generalized Linear Models menu, and that would give me the estimated values for the coefficients c, a1 and a2 that I want.

In R, I tried the following:

glm(y ~ x + Test*x)

This gives me the following coefficients: Intercept (c), x, Test, and x:Test.

I think that the 'Test' coefficient is a modifier to the intercept (which I don't want to fit), whilst x:Test is the modifier to the coefficient of x (i.e. a2) that I actually want.

How can I avoid fitting the coefficient for 'Test' in R? Is there an operator that will have the same effect as my use of '.' in GenStat?

Edit: I also tried:

glm(y ~ x + offset(Test*x))

But that just gives me a fixed offset of the value 'Test*x', it does not fit the coefficient 'a2'.


Solution

  • I think I've managed to find the solution.

    The correct operator in R is ':'.

    So the code in R is:

    glm(y~x+Test:x)

    And that gives me fitted coefficients for the intercept, x and Test:x, as required.