当前位置: 动力学知识库 > 问答 > 编程问答 >

r - Why can't I add a one level factor in my lm?

问题描述:

I am performing a regression and I want to add a year as factor, but my year is just one year (2010) and when I run the equation I get an error:

contrasts<- (tmp, value = contr.funs[1 + isOF[nn]]) :

contrasts can be applied only to factors with 2 or more levels

My equation:

Lanu <- lm(YFT ~ PR1*factor(DN1)*factor(NTM1)*factor(AÑO1))

All my factors have the same length, YFT is density and PR1 is depth, factor DN1 is two types of net, and NTM1 is three locations. I want to know if there is interaction in my factors.

网友答案:

Since you only have 1 year, year would just be a constant. There would be no identifying variation, which is what @MrFlick is implying in his comment. That's why you get an error if you try a contrast or if you were to try putting it in the lm equation.

Realistically, you should not include year since you do not have any variation in years.

Technically, you could include it in your regression if you were to omit the default constant (commonly called "Beta naught") which is calculated as the mean response when all predictor values are zero. That would be the same as artifically setting your y-intercept to 2010.

It's hard to think of a scenario where that would be advantageous and even then you could not have interaction terms with a constant.

Here are your options:

  1. Forget about including year
  2. Get more years
  3. Use 2010 as your y-intercept: Lanu <- lm(YFT ~ 0 + PR1*factor(DN1)*factor(NTM1)*factor(AÑO1), offset=rep(2010, length(YFT))
  4. Use months, quarters, or some subdivision of the year instead of year

Note that adding 0 + or - 1 to the equation removes the normal intercept and offset creates the artificial year intercept.

分享给朋友:
您可能感兴趣的文章:
随机阅读: