Results 1 to 3 of 3
  1. - Top - End - #1
    Ogre in the Playground
     
    BardGuy

    Join Date
    Jan 2009

    Default R: plotting values a regression model produces

    I have a regression model in R, and I want to make a plot of the actual values against the values the model yields. For example, if my real data is 12 at data point 1 but the model yields a value of 13.5 at data point 1, I want to overlay those two plots together, so I can visually see how close to the real data my model is.

    I know I've done this before, but I can't remember how to do it.
    Getting my real data is easy, via something like
    ts.plot(x) or plot(x)
    but I can't figure out how to overlay the regression model.

    I realize I can create a vector by putting each x value through my model and getting what it would yield, but it would be a real pain to do that (at least using any methods I'm aware of), since I have both the month of the year and the week of the month as factors.
    Which makes me want to ask: is there an easy way to tell R "Here is my model and my x-values. Tell me what my model says for these X values."

    R and SAS are the only languages I have access to for this. I'd take an answer in SAS, but R is preferable as that's where I derived my model. I tried Google, but the answers I'm getting aren't to the question I intend to ask.

  2. - Top - End - #2
    Barbarian in the Playground
     
    NecromancerGuy

    Join Date
    Apr 2010
    Location
    Night Vale
    Gender
    Male

    Default Re: R: plotting values a regression model produces

    Quote Originally Posted by JeenLeen View Post
    I have a regression model in R, and I want to make a plot of the actual values against the values the model yields. For example, if my real data is 12 at data point 1 but the model yields a value of 13.5 at data point 1, I want to overlay those two plots together, so I can visually see how close to the real data my model is.

    I know I've done this before, but I can't remember how to do it.
    Getting my real data is easy, via something like
    ts.plot(x) or plot(x)
    but I can't figure out how to overlay the regression model.

    I realize I can create a vector by putting each x value through my model and getting what it would yield, but it would be a real pain to do that (at least using any methods I'm aware of), since I have both the month of the year and the week of the month as factors.
    Which makes me want to ask: is there an easy way to tell R "Here is my model and my x-values. Tell me what my model says for these X values."

    R and SAS are the only languages I have access to for this. I'd take an answer in SAS, but R is preferable as that's where I derived my model. I tried Google, but the answers I'm getting aren't to the question I intend to ask.
    plot(x)
    abline(LinearModel_object) reference
    Not going to type out argurments for curve(...) on my phone, but that should get you started.
    Avatar by TheGiant
    B.S. in Statistics - University of Minnesota <- as of May 2017, still pretty excited : )
    Long-form Sig

  3. - Top - End - #3
    Ogre in the Playground
     
    BardGuy

    Join Date
    Jan 2009

    Default Re: R: plotting values a regression model produces

    Thanks, but I'm still not getting it to work. Let me lay out what I have.

    My model, called timeReg, is made by:
    timeReg=lm(d ~ as.factor(week) + as.factor(month) + dummy -1, data=combo)

    where week is the week number (1-5) in a given month, month is the month number (1-12) in the year, and dummy is to compensate for an outlier (it is 1 at the outlier, 0 otherwise). All are vectors combined into one time series with the raw data, called d.

    ts.plot(d) gives me the original data.
    But plot(timeReg) gives diagnostics, curve gives error message, and abline(timeReg) gives a horizontal line.

    For curve, I tried curve(timeReg), curve(timeReg$fit), and curve(timeReg$model).

    HOWEVER, between starting this post and now I went through some notes and found the code I used previously, which works.
    Code:
    > ts.plot(d, col='red')
    > lines(as.vector(time(d)), fitted(timeReg, col='blue'))
    Thanks for your help, though. I wonder if it's something about the time series nature that's disrupting the functions that usually work.
    Last edited by JeenLeen; 2017-12-06 at 10:13 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •