Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of offset in lm regression - R

Tags:

r

lm

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?

like image 263
ac2051 Avatar asked Jun 04 '13 14:06

ac2051


People also ask

What is an offset in linear regression?

" An offset is a component of a linear predictor that is known in advance (typically from theory, or from a mechanistic model of the process). " Because it is known, it requires no parameter to be estimated from the. data.

What is offset in Rstudio?

An offset is a term to be added to a linear predictor, such as in a generalised linear model, with known coefficient 1 rather than an estimated coefficient.

What is offset in data analysis?

Offset is a variable which used in Poisson Regression Analysis. This analysis is used whenever the data is recorded over an observed period. Eg: Number of Customers who arrive at a restaurant in one hour, Number of trees in a square unit area.

What does offset mean in machine learning?

An offset is a per-row “bias value” that is used during model training. For Gaussian distributions, offsets can be seen as simple corrections to the response (y) column. Instead of learning to predict the response (y-row), the model learns to predict the (row) offset of the response column.


2 Answers

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)
like image 170
Hong Ooi Avatar answered Sep 17 '22 22:09

Hong Ooi


In fact, the real issue here is that you should specify offset with a vector whose length is the same as the number of rows (or the length, if data is composed as a vector) of your data. The following code will do your job as expected:

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

Here is a good explanation for those who are interested: http://rfunction.com/archives/223

like image 39
Liang Zhang Avatar answered Sep 17 '22 22:09

Liang Zhang