Murat Tasan
2013-Aug-27 03:46 UTC
[R] predict.glm with constant non-zero response intercept
hi all -- i'm running into a strange problem that i can't seem to easily get around, but i'm probably just missing something obvious. i have a model to which some data is fit using glm with no intercept term (using my data variables "x" and "y" and a specific link function "mylink"): ########################################> foo.glm <- glm(y ~ x + 0, family = Gamma(link = mylink)) > foo.glmCall: glm(formula = y ~ x + 0, family = Gamma(link = mylink)) Coefficients: x 8.643e-05 Degrees of Freedom: 272 Total (i.e. Null); 271 Residual Null Deviance: 1821 Residual Deviance: 1135 AIC: -969> predict(foo.glm, newdata = data.frame(x = c(10e3, 20e3)))1 2 0.8642988 1.7285976 ######################################## so far, everything is great. now i'd like to force a constraint on my model: an particular (fixed) non-zero response variable intercept term, let's call it 0.5. the only method i know of to accomplish this is to use an offset term (either in the formula, or with the offset parameter to glm), like so: ########################################> foo.glm <- glm(y ~ x + 0, offset = rep(0.5, length(y)), family = Gamma(link = mylink)) > foo.glmCall: glm(formula = y ~ x + 0, family = Gamma(link = mylink), offset = rep(0.5, length(y))) Coefficients: x 8.019e-05 Degrees of Freedom: 272 Total (i.e. Null); 271 Residual Null Deviance: 1639 Residual Deviance: 1131 AIC: -970.5 ######################################## now i'd like to try predict(...) again, but clearly predict.glm(...) wants to add (actually subtract) the full offset vector used in the glm(...) call explicitly to my new predictions, which forces recycling and gives me something like this: ########################################> predict(foo.glm, newdata = data.frame(x = c(10e3, 20e3)))[1] 1.301893 2.103787 1.301893 2.103787 1.301893 2.103787 1.301893 2.103787 1.301893 [10] 2.103787 1.301893 2.103787 1.301893 2.103787 1.301893 2.103787 1.301893 2.103787 ... [262] 2.103787 1.301893 2.103787 1.301893 2.103787 1.301893 2.103787 1.301893 2.103787 [271] 1.301893 2.103787 ######################################## furthermore, with some additional glm-'like' packages (e.g. betareg), the differing lengths of 'newdata' and the original 'offset' vectors produce full-on errors. is there any way in formula(...) other than including offset(...) to force a CONSTANT non-zero intercept? thanks for any insight! -m
Michael Dewey
2013-Aug-27 10:11 UTC
[R] predict.glm with constant non-zero response intercept
At 04:46 27/08/2013, Murat Tasan wrote:>hi all -- i'm running into a strange problem that i can't seem to >easily get around, but i'm probably just missing something obvious.I think you just subtract it from y. But perhaps I too am missing the obvious.>i have a model to which some data is fit using glm with no intercept >term (using my data variables "x" and "y" and a specific link function >"mylink"): > >######################################## > > foo.glm <- glm(y ~ x + 0, family = Gamma(link = mylink)) > > foo.glm > >Call: glm(formula = y ~ x + 0, family = Gamma(link = mylink)) > >Coefficients: > x >8.643e-05 > >Degrees of Freedom: 272 Total (i.e. Null); 271 Residual >Null Deviance: 1821 >Residual Deviance: 1135 AIC: -969 > > predict(foo.glm, newdata = data.frame(x = c(10e3, 20e3))) > 1 2 >0.8642988 1.7285976 >######################################## > >so far, everything is great. >now i'd like to force a constraint on my model: an particular (fixed) >non-zero response variable intercept term, let's call it 0.5. >the only method i know of to accomplish this is to use an offset term >(either in the formula, or with the offset parameter to glm), like so: > >######################################## > > foo.glm <- glm(y ~ x + 0, offset = rep(0.5, length(y)), family = > Gamma(link = mylink)) > > foo.glm > >Call: glm(formula = y ~ x + 0, family = Gamma(link = mylink), >offset = rep(0.5, > length(y))) > >Coefficients: > x >8.019e-05 > >Degrees of Freedom: 272 Total (i.e. Null); 271 Residual >Null Deviance: 1639 >Residual Deviance: 1131 AIC: -970.5 >######################################## > >now i'd like to try predict(...) again, but clearly predict.glm(...) >wants to add (actually subtract) the full offset vector used in the >glm(...) call explicitly to my new predictions, which forces recycling >and gives me something like this: > >######################################## > > predict(foo.glm, newdata = data.frame(x = c(10e3, 20e3))) > [1] 1.301893 2.103787 1.301893 2.103787 1.301893 2.103787 1.301893 >2.103787 1.301893 > [10] 2.103787 1.301893 2.103787 1.301893 2.103787 1.301893 2.103787 >1.301893 2.103787 >... >[262] 2.103787 1.301893 2.103787 1.301893 2.103787 1.301893 2.103787 >1.301893 2.103787 >[271] 1.301893 2.103787 >######################################## > >furthermore, with some additional glm-'like' packages (e.g. betareg), >the differing lengths of 'newdata' and the original 'offset' vectors >produce full-on errors. > >is there any way in formula(...) other than including offset(...) to >force a CONSTANT non-zero intercept? > >thanks for any insight! > >-mMichael Dewey info at aghmed.fsnet.co.uk http://www.aghmed.fsnet.co.uk/home.html