0

Using the following data:

http://pastebin.com/4wiFrsNg

I am wondering how to fit a predefined offset to the raw relationship of another model i.e. how to fit the estimates from Model A, thus:

ModelA<-lm(Dependent1~Explanatory)

to model B thus:

ModelB<-lm(Dependent2~Explanatory)

Where the explanatory variable is either the variable "Categorical" in my dataset, or the variable "Continuous". I got a useful answer related to a similar question on CV:

https://stats.stackexchange.com/questions/62584/how-to-fit-a-specific-model-to-some-data

Here the exaplantory variable was "Continuous". However I had some extra questions I needed answering that I thought might be more suited to SO. If this is not the case, tell me and I will delete this question :)

Specifically, I was advised in the link above that in order to fit a predefined slope for the continuous explanatory variable in my dataset I should do this:

lm( Dependent2 ~ 1 + offset( Slope * Continuous ) )

Where slope is the predefined slope taken from from Model A. Which worked great.

Now I am wondering, how do I do the same when x is a categorical variable with two levels, and then when x is a continuous variable with a quadratic term i.e. x+x^2?

For the quadratic term I am trying:

lm( Dependent2 ~ 1 + offset( Slope * Continuous )+ offset( Slope2 * I((Continuous)^2)) )

Where Slope is the value for the fixed estimate of for Continuous term, and Slope2 is the value for the fixed estimate of the quadratic term.

I am unsure how to get this working for a categorical variable however. When I try to fit an offset as:

lm( Dependent2 ~ 1 + offset( Slope * Categorical ) )

where again, slope is the slope value of the fixed estimate taken from Model A, I get an error:

"Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) :   0 (non-NA) cases
In addition: Warning message:
In Ops.factor(0.25773, Categorical) : * not meaningful for factors"

If anyone has an input on how to create offsets for categorical variables it would be greatly appreciated :)

Community
  • 1
  • 1
Sarah
  • 789
  • 3
  • 12
  • 29
  • I guess `offset` wants the factor dummy encoded. Better answers can be given with [reproducible code and data](http://stackoverflow.com/a/5963610/1412059). – Roland Jun 26 '13 at 13:32
  • Hi Roland, I have added some raw data and clarified the explanation. I hope this helps. – Sarah Jun 26 '13 at 14:07

1 Answers1

2

The best you can probably do is compute the offset manually for each level of your factor:

x <- rep(1:3, each=10)
df <- data.frame(x=factor(x), y=3 - x)

# compute the offset for each level of x
df$o <- with(df, ifelse(x == "1", 2, ifelse(x == "2", 1, 0)))

# fitted coef's for the models below will all be zero due to presence of offset
lm(y - o ~ x - 1, data=df)
# or
lm(y ~ x + offset(o), data=df)
Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
  • Thank you for your answer. I have tried this for the data in my post (which I have called "data") thus: data$o <- with(data, ifelse(Categorical == "Y", 0.25773, ifelse(Categorical == "N",-0.25773))) But I am getting the error: "Error in ifelse(Categorical == "N",-0.25773) : argument "no" is missing, with no default" Do you know of a way to resolve this? Thanks so much! – Sarah Jun 26 '13 at 14:25
  • If `Categorical` only has 2 levels, then you only need one `ifelse`. Notice how there are two options: "if" and "else".... – Hong Ooi Jun 26 '13 at 14:28
  • When you fit `y - o ~ x - 1`, the estimates for `x` will be adjusted for the offset. For example, suppose the mean `y` for level 2 of `x` is 10. Without an offset, you would see an estimated coefficient of 10. If your offset value is, say, 8, the estimate would instead be 2. In general, though, you would not want to include an `x` whose effect is already included in the offset -- I put that in the post mostly for illustrative purposes. – Hong Ooi Jun 26 '13 at 15:58
  • So, tl;dr: you probably want just `y ~ offset(o)`. Apologies for the confusion. – Hong Ooi Jun 26 '13 at 16:00