Search code examples
rlm

Use of offset in lm regression - R


I have this code

dens <- read.table('DensPiu.csv', header = FALSE)
fl <- read.table('FluxPiu.csv', header = FALSE)
mydata <- data.frame(c(dens),c(fl))

dat = subset(mydata, dens>=3.15)
colnames(dat) <- c("x", "y")
attach(dat)

and I would like to do a least-square regression on the data contained in dat, the function has the form

y ~ a + b*x

and I want the regression line to pass through a specific point P(x0,y0) (which is not the origin).

I'm trying to do it like this

 x0 <- 3.15 

 y0 <-283.56

 regression <- lm(y ~ I(x-x0)-1, offset=y0)

(I think that data = dat is not necessary in this case) but I get this error :

Error in model.frame.default(formula = y ~ I(x - x0) - 1, : variable
 lengths differ (found for '(offset)').

I don't know why. I guess that I haven't defined correctly the offset value but I couldn't find any example online.

Could someone explain to me how offset works, please?


Solution

  • Your offset term has to be a variable, like x and y, not a numeric constant. So you need to create a column in your dataset with the appropriate values.

    dat$o <- 283.56
    lm(y ~ I(x - x0) - 1, data=dat, offset=o)