在lm回归中使用偏移量-R [英] Use of offset in lm regression - R
问题描述
我有这个程序
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)
我想对 dat 中包含的数据进行最小二乘回归,该函数的形式为
and I want to do a least-square regression on the data contained in dat, the function has the form
y ~ a + b*x
,我希望回归线穿过特定点P(x0,y0)(不是原点).
and I want the regression line to pass through a specific point P(x0,y0) (which is not the origin).
我正在尝试这样做
x0 <- 3.15
y0 <-283.56
regression <- lm(y ~ I(x-x0)-1, offset=y0)
(我认为在这种情况下,data = dat并不是必需的),但是我有此错误:
(I think that data = dat is not necessary in this case) but I have 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 on the internet.
有人可以向我解释偏移量的工作方式吗?
Can anybody explain me how offset works please?
推荐答案
您的偏移项必须是变量,例如x
和y
,而不是数字常量.因此,您需要在数据集中使用适当的值创建一列.
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)
这篇关于在lm回归中使用偏移量-R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!