-1

My data is currently in a data frame with 3 columns (y, x, season) where season is a factor ("SPRING","SUMMER","FALL","WINTER")

I fit a linear model

yi = b0 + b.xi + ei ~ N(0,sig^2)
lm(y ~ x)

i want to test this against an extended model of the form

yij = b0 + bj.xij + eij     

where j (= 1:4) is the SEASON of the year

Usually i would just create the design matrix, X and then b = inv(X'X).(X'Y)

However i'd like to do this in R using lm()

How can i use lm() to fit this extended model with the data in its current format? Do i HAVE to transform my data to wide format with 5 columns (y, x1, x2, x3, x4 ) where xj represents the measurements for season j?

PKumar
  • 10,971
  • 6
  • 37
  • 52
andy
  • 185
  • 9
  • 1
    Look up how to add categorical variables to a regression model in R, try [here](https://stackoverflow.com/questions/30159162/linear-model-with-categorical-variables-in-r) or [here](https://www.r-bloggers.com/the-lm-function-with-categorical-predictors/) – astrofunkswag Sep 10 '19 at 20:04
  • Maybe `plm(......,index = c("season"))` (if I don't understand you wrongly). I think you are not seeking for dummy variable input. – maydin Sep 10 '19 at 20:08
  • do you just want ann interaction term: `lm(y ~ x* season)` – user20650 Sep 10 '19 at 21:02
  • @astrofunkswag incorrect. This is not modelled as an effect – andy Sep 11 '19 at 15:35

1 Answers1

0

So i can keep my data in its original format: data frame with 3 columns (y, x, SEASON) where SEASON is a factor.

the model is:

yij = b0 + bj.xij + eij

where j (= 1:4) is the SEASON of the year

and the R call is:

lm(y ~ x:SEASON)

This is equivalent of fitting multiple independent lines (multiple b's) with a common intercept where each line represents a SEASONal relationship.

andy
  • 185
  • 9