Dear all, I would like to use predict.lm() with an existing lm object but with new arbitrary coefficients. I modify 'fit$coef' (see example below) "by hand" but the actual model in 'fit' used for prediction does not seem to be altered (although fit$coef is!). Can anyone please help me do this properly? Thanks in advance, J?r?mie> dat <- data.frame(y=c(0,25,32,15), x=as.factor(c(1,1,2,2))) > fit <- lm(y ~ x, data=dat) > fitCall: lm(formula = y ~ x, data = dat) Coefficients: (Intercept) x2 12.5 11.0> fit$coef[[2]] <- 100 > dat.new <- data.frame(x=as.factor(c(1,2,1,2))) > predict.lm(fit, dat.new)1 2 3 4 12.5 23.5 12.5 23.5> fitCall: lm(formula = y ~ x, data = dat) Coefficients: (Intercept) x2 12.5 11.0> fit$coef(Intercept) x2 12.5 100.0>J?r?mie Lebrec Dept. of Medical Statistics and Bioinformatics Leiden University Medical Center Postzone S-05-P P.O. Box 9600 2300 RC Leiden The Netherlands j.j.p.lebrec at lumc.nl
Hi, names(fit) fit$coefficients[[2]] <- 100 -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25'' 40" S 49° 16'' 22" O On 28/08/07, J.J.P.Lebrec@lumc.nl <J.J.P.Lebrec@lumc.nl> wrote:> > Dear all, > > I would like to use predict.lm() with an existing lm object but with new > arbitrary coefficients. I modify ''fit$coef'' (see example below) "by hand" > but the actual model in ''fit'' used for prediction does not seem to be > altered (although fit$coef is!). > > Can anyone please help me do this properly? > > Thanks in advance, > > Jérémie > > > > > dat <- data.frame(y=c(0,25,32,15), x=as.factor(c(1,1,2,2))) > > fit <- lm(y ~ x, data=dat) > > fit > > Call: > lm(formula = y ~ x, data = dat) > > Coefficients: > (Intercept) x2 > 12.5 11.0 > > > fit$coef[[2]] <- 100 > > dat.new <- data.frame(x=as.factor(c(1,2,1,2))) > > predict.lm(fit, dat.new) > 1 2 3 4 > 12.5 23.5 12.5 23.5 > > fit > > Call: > lm(formula = y ~ x, data = dat) > > Coefficients: > (Intercept) x2 > 12.5 11.0 > > > fit$coef > (Intercept) x2 > 12.5 100.0 > > > > > > Jérémie Lebrec > Dept. of Medical Statistics and Bioinformatics > Leiden University Medical Center > Postzone S-05-P > P.O. Box 9600 > 2300 RC Leiden > The Netherlands > j.j.p.lebrec@lumc.nl > > ______________________________________________ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
It is fit$coefficients, not fit$coef .>From the help page:name: A literal character string or a name (possibly backtick quoted). For extraction, this is normally (see under Environments) partially matched to the 'names' of the object. Note the qualifier 'for extraction', so you assigned a new element with name 'coef', and predict.lm used fit$coefficients. On Tue, 28 Aug 2007, J.J.P.Lebrec at lumc.nl wrote:> Dear all, > > I would like to use predict.lm() with an existing lm object but with new arbitrary coefficients. I modify 'fit$coef' (see example below) "by hand" but the actual model in 'fit' used for prediction does not seem to be altered (although fit$coef is!). > > Can anyone please help me do this properly? > > Thanks in advance, > > J?r?mie > > > >> dat <- data.frame(y=c(0,25,32,15), x=as.factor(c(1,1,2,2))) >> fit <- lm(y ~ x, data=dat) >> fit > > Call: > lm(formula = y ~ x, data = dat) > > Coefficients: > (Intercept) x2 > 12.5 11.0 > >> fit$coef[[2]] <- 100 >> dat.new <- data.frame(x=as.factor(c(1,2,1,2))) >> predict.lm(fit, dat.new) > 1 2 3 4 > 12.5 23.5 12.5 23.5 >> fit > > Call: > lm(formula = y ~ x, data = dat) > > Coefficients: > (Intercept) x2 > 12.5 11.0 > >> fit$coef > (Intercept) x2 > 12.5 100.0 >> > > > > J?r?mie Lebrec > Dept. of Medical Statistics and Bioinformatics > Leiden University Medical Center > Postzone S-05-P > P.O. Box 9600 > 2300 RC Leiden > The Netherlands > j.j.p.lebrec at lumc.nl > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
The important element in the fit object is named coefficients not coef. Sometimes the partial matching can cause confusion when it tries to help. In your case it creates a copy of the coefficients, changes the 2nd value, then creates a new component to fit called coef with these values (not changing the original coefficients). Since the predict, print, summary, etc. functions use the coefficients element rather than your new coef element, your changes are silently ignored. One option is to spell out coefficients fully to replace that (though that could end up breaking other things), another possible option is to create the model using offsets (but I'm not sure how that would work in this case). Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at intermountainmail.org (801) 408-8111> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of > J.J.P.Lebrec at lumc.nl > Sent: Tuesday, August 28, 2007 7:33 AM > To: r-help at stat.math.ethz.ch > Subject: [R] Forcing coefficients in lm object > > Dear all, > > I would like to use predict.lm() with an existing lm object > but with new arbitrary coefficients. I modify 'fit$coef' (see > example below) "by hand" but the actual model in 'fit' used > for prediction does not seem to be altered (although fit$coef is!). > > Can anyone please help me do this properly? > > Thanks in advance, > > J?r?mie > > > > > dat <- data.frame(y=c(0,25,32,15), x=as.factor(c(1,1,2,2))) fit <- > > lm(y ~ x, data=dat) fit > > Call: > lm(formula = y ~ x, data = dat) > > Coefficients: > (Intercept) x2 > 12.5 11.0 > > > fit$coef[[2]] <- 100 > > dat.new <- data.frame(x=as.factor(c(1,2,1,2))) > > predict.lm(fit, dat.new) > 1 2 3 4 > 12.5 23.5 12.5 23.5 > > fit > > Call: > lm(formula = y ~ x, data = dat) > > Coefficients: > (Intercept) x2 > 12.5 11.0 > > > fit$coef > (Intercept) x2 > 12.5 100.0 > > > > > > J?r?mie Lebrec > Dept. of Medical Statistics and Bioinformatics Leiden > University Medical Center Postzone S-05-P P.O. Box 9600 2300 > RC Leiden The Netherlands j.j.p.lebrec at lumc.nl > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >