gatemaze at gmail.com
2008-Feb-19 14:41 UTC
[R] fitted values are different from manually calculating
Hello, on a simple linear model the values produced from the fitted(model) function are difference from manually calculating on calc. Will anyone have a clue... or any insights on how fitted function calculates the values? Thank you. -- -- Yianni [[alternative HTML version deleted]]
john seers (IFR)
2008-Feb-19 14:51 UTC
[R] fitted values are different from manually calculating
Hi A simple example of a linear model: x<-1:10 y<-3*x+1 m1<-lm(y~x) y # [1] 4 7 10 13 16 19 22 25 28 31 fitted(m1) # 1 2 3 4 5 6 7 8 9 10 # 4 7 10 13 16 19 22 25 28 31 The fitted and calculated values look identical to me. Can you give an example of how your calculated values do not match? Or have I misunderstood your question? Regards JS --- -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of gatemaze at gmail.com Sent: 19 February 2008 14:41 To: r-help at stat.math.ethz.ch Subject: [R] fitted values are different from manually calculating Hello, on a simple linear model the values produced from the fitted(model) function are difference from manually calculating on calc. Will anyone have a clue... or any insights on how fitted function calculates the values? Thank you. -- -- Yianni [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org 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.
Dimitris Rizopoulos
2008-Feb-19 14:57 UTC
[R] fitted values are different from manually calculating
why do you say this? Check for instance the following code: x1 <- runif(100, -5, 5) x2 <- rnorm(100) x3 <- rep(0:1, each = 50) y <- 2 + 0.5 * x1 + 2 * x2 - 3 * x3 + rnorm(100) fit <- lm(y ~ x1 + x2 + x3) all.equal( as.vector(fitted(fit)), as.vector(model.matrix(fit) %*% coef(fit)) ) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: <gatemaze at gmail.com> To: <r-help at stat.math.ethz.ch> Sent: Tuesday, February 19, 2008 3:41 PM Subject: [R] fitted values are different from manually calculating> Hello, > > on a simple linear model the values produced from the fitted(model) > function > are difference from manually calculating on calc. Will anyone have a > clue... > or any insights on how fitted function calculates the values? Thank > you. > > -- > -- Yianni > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Douglas Bates
2008-Feb-19 15:01 UTC
[R] fitted values are different from manually calculating
On Feb 19, 2008 8:41 AM, <gatemaze at gmail.com> wrote> on a simple linear model the values produced from the fitted(model) function > are difference from manually calculating on calc. Will anyone have a clue... > or any insights on how fitted function calculates the values? Thank you.______________________________________________> R-help at r-project.org 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.As the posting guide (you did read the posting guide, didn't you?) says, it helps if you can provide a reproducible example. Otherwise, we can only guess at what you mean. Try taking a fitted model and examining model.matrix(model) and coef(model) It should be the case that model.matrix(model) %*% coef(model) is reasonably close to fitted(model). I just saw that Dimitris wrote almost the same thing but, since I have written it, I'll mail it anyway.
Mark Difford
2008-Feb-19 21:24 UTC
[R] fitted values are different from manually calculating
Hi Yianni, This just proves that you should be using R as your calculator, and not the other one! Regards, Mark. gatemaze wrote:> > Hello, > > on a simple linear model the values produced from the fitted(model) > function > are difference from manually calculating on calc. Will anyone have a > clue... > or any insights on how fitted function calculates the values? Thank you. > > -- > -- Yianni > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org 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. > >-- View this message in context: http://www.nabble.com/fitted-values-are-different-from-manually-calculating-tp15568106p15569205.html Sent from the R help mailing list archive at Nabble.com.