I'm trying to plot a time series (quarterly observations, seasonal effect, T=56),regression line and predicted point on the same plot. I'm using following commands:> plot(qdts,xlim=c(1982,1997)) > lines(fitted(m2)~time,data=qd,col="red") > points(predict(m2,newdata=nqd),col="blue")where: qdts<-ts(qd$durables,start=c(1982,1),freq=4) m2<-lm(qdts~time+quar) time<-seq(1,length(qdts),1) nqd<-data.frame(time=c(57),quar=c(1)) Unfortunately once I enter all three commands the fitted line and predicted point don't appear on the plot. What surprises me is that I am not getting any error message. What I am doing wrong? Many thanks in advance Ula -- View this message in context: http://www.nabble.com/Plotting-Standard-Regression-Fit-Am-I-wrong-or-a-bug--tp20399460p20399460.html Sent from the R help mailing list archive at Nabble.com.
Daniel Malter
2008-Nov-08 20:16 UTC
[R] Plotting Standard Regression Fit-Am I wrong or a bug?
Please provide a self-contained example (small simulated dataset or the original data [if it is or reasonable size] such that it can be copy-pasted in R). We cannot tell what is going wrong when we can't follow what is going on. Generally, that you don't get any error message indicates that the lines and points commands are actually executed. So nothing is probably wrong with them. It then comes down to whether you plot within the right limits. You might want to test different "ylim" and "xlim" arguments in your plot() command to see whether your "lines" and "points" fall outside the limits that are initialized when you call the plot command. [SEE EXAMPLE BELOW] Cheers, Daniel ##Self-contained R-example x=rnorm(100,0,1) e=rnorm(100,0,1) y=x+e plot(y,xlim=c(101,120)) ##plot y #101 to y #120 #no points plotted because there are only 100 Ys! reg=lm(y~x) points(fitted(reg)) ##tries to plot fitted(reg) versus index numbers 1 to 100 ##in the plot initialized above ##but fitted #1 to fitted #120 ##is still out of bounds of the data, there are only 100 Ys, ##so there are only 100 fitted Ys (Y hats) ##plot correctly plot(y~x,col="red",xlim=c(-8,8),ylim=c(-8,8)) points(fitted(reg)~x,col="blue") ------------------------- cuncta stricte discussurus ------------------------- -----Urspr?ngliche Nachricht----- Von: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Im Auftrag von uep Gesendet: Saturday, November 08, 2008 2:19 PM An: r-help at r-project.org Betreff: [R] Plotting Standard Regression Fit-Am I wrong or a bug? I'm trying to plot a time series (quarterly observations, seasonal effect, T=56),regression line and predicted point on the same plot. I'm using following commands:> plot(qdts,xlim=c(1982,1997)) > lines(fitted(m2)~time,data=qd,col="red") > points(predict(m2,newdata=nqd),col="blue")where: qdts<-ts(qd$durables,start=c(1982,1),freq=4) m2<-lm(qdts~time+quar) time<-seq(1,length(qdts),1) nqd<-data.frame(time=c(57),quar=c(1)) Unfortunately once I enter all three commands the fitted line and predicted point don't appear on the plot. What surprises me is that I am not getting any error message. What I am doing wrong? Many thanks in advance Ula -- View this message in context: http://www.nabble.com/Plotting-Standard-Regression-Fit-Am-I-wrong-or-a-bug-- tp20399460p20399460.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.
uep wrote:> > I'm trying to plot a time series (quarterly observations, seasonal effect, > T=56),regression line and predicted point on the same plot. > I'm using following commands: >> plot(qdts,xlim=c(1982,1997)) >> lines(fitted(m2)~time,data=qd,col="red") >> points(predict(m2,newdata=nqd),col="blue") > > where: > qdts<-ts(qd$durables,start=c(1982,1),freq=4) > m2<-lm(qdts~time+quar) > time<-seq(1,length(qdts),1) > nqd<-data.frame(time=c(57),quar=c(1)) > > Unfortunately once I enter all three commands the fitted line and > predicted point don't appear on the plot. What surprises me is that I am > not getting any error message. > > What I am doing wrong? > Many thanks in advance > > Ula > >Thanks. This is the qd dataset: year quarter durables 1 1982 1 3852 2 1982 2 3480 3 1982 3 4226 4 1982 4 3881 5 1983 1 4738 6 1983 2 4164 7 1983 3 5006 8 1983 4 4342 9 1984 1 4766 10 1984 2 4485 11 1984 3 4757 12 1984 4 4630 13 1985 1 5082 14 1985 2 4606 15 1985 3 5560 16 1985 4 4918 17 1986 1 5588 18 1986 2 5226 19 1986 3 6323 20 1986 4 5835 21 1987 1 6343 22 1987 2 5859 23 1987 3 7446 24 1987 4 6775 25 1988 1 7821 26 1988 2 7146 27 1988 3 9476 28 1988 4 7945 29 1989 1 9012 30 1989 2 8147 31 1989 3 9971 32 1989 4 8284 33 1990 1 9085 34 1990 2 7937 35 1990 3 9714 36 1990 4 7940 37 1991 1 8323 38 1991 2 7028 39 1991 3 9201 40 1991 4 7788 41 1992 1 8120 42 1992 2 7228 43 1992 3 9475 44 1992 4 8245 45 1993 1 8758 46 1993 2 7496 47 1993 3 10861 48 1993 4 8892 49 1994 1 9592 50 1994 2 8344 51 1994 3 11410 52 1994 4 9563 53 1995 1 10172 54 1995 2 8627 55 1995 3 12066 56 1995 4 10007 -- View this message in context: http://www.nabble.com/Plotting-Standard-Regression-Fit-Am-I-wrong-or-a-bug--tp20399460p20400227.html Sent from the R help mailing list archive at Nabble.com.
Peter Dalgaard
2008-Nov-08 20:46 UTC
[R] Plotting Standard Regression Fit-Am I wrong or a bug?
uep wrote:> I'm trying to plot a time series (quarterly observations, seasonal effect, > T=56),regression line and predicted point on the same plot. > I'm using following commands: >> plot(qdts,xlim=c(1982,1997)) >> lines(fitted(m2)~time,data=qd,col="red") >> points(predict(m2,newdata=nqd),col="blue")Umm, as far as I can see you have a plot with xlim from 1982 to 1987, and your lines(..~time) has time==1:something.> where: > qdts<-ts(qd$durables,start=c(1982,1),freq=4) > m2<-lm(qdts~time+quar) > time<-seq(1,length(qdts),1) > nqd<-data.frame(time=c(57),quar=c(1)) > > Unfortunately once I enter all three commands the fitted line and predicted > point don't appear on the plot. What surprises me is that I am not getting > any error message. > > What I am doing wrong? > Many thanks in advance > > Ula >-- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Thank you Daniel, I did try to change xlim's and ylim's, but the fitted line still doesn't appear on the plot. Below is a small expample I've created (I'm sorry for using names, but I het confused with x's and y's):>mat<-matrix(c(1,2,3,4,5,6,7,8,1,2,3,4,1,2,3,4,1,5,2,7,3,7,5,8),nrow=8,ncol=3) >mats<-ts(mat[,3],start=c(1,1),freq=4) >quart<-mat[,2] >time<-seq(1,length(mats),1) >nmat<-data.frame(time=c(9),quart=c(1)) >quar<-factor(nmat$quart) >t1<-lm(mats~time) >plot(mats,xlim=c(1,3.25),ylim=c(0,15)) >lines(fitted(t1)~time,col="blue") >points(c(3),predict(t1,newdata=nmat),col="red")Unfortunately for me, this example works and does plot fitted line and predicted points. I really don't understand why I can't make the same commands work when trying with my real life data. Anyway, thanks for getting back to me earlier on. Regards Ula uep wrote:> > I'm trying to plot a time series (quarterly observations, seasonal effect, > T=56),regression line and predicted point on the same plot. > I'm using following commands: >> plot(qdts,xlim=c(1982,1997)) >> lines(fitted(m2)~time,data=qd,col="red") >> points(predict(m2,newdata=nqd),col="blue") > > where: > qdts<-ts(qd$durables,start=c(1982,1),freq=4) > m2<-lm(qdts~time+quar) > time<-seq(1,length(qdts),1) > nqd<-data.frame(time=c(57),quar=c(1)) > > Unfortunately once I enter all three commands the fitted line and > predicted point don't appear on the plot. What surprises me is that I am > not getting any error message. > > What I am doing wrong? > Many thanks in advance > > Ula > >-- View this message in context: http://www.nabble.com/Plotting-Standard-Regression-Fit-Am-I-wrong-or-a-bug--tp20399460p20401091.html Sent from the R help mailing list archive at Nabble.com.