Skip to content

plot output function

5 messages · cp133, Uwe Ligges, Martin Maechler +1 more

#
Hi,

I have written my own function to estimate a local linear regression.
When I plot the output vector on the scatterplot of the data, the
function lines does not work and I get a rescaled regression line. 
When I plot the output vector alone, the graph looks right, but choosing
the type "line" instead of the default does not deliver a clear single
line.

Does somebody know why this is happening.

Thank you,

Chiara
#
cp133 wrote:
Does abline() help?

If not, please be more specific in your question and tell us what you 
are exactly going to do.

Uwe Ligges
#
no, I'm afraid abline does not help, it works only for straight lines.

The output of my function is a vector of fitted values (called fit).
I would like to superimpose the fitted regression line to the
scatterplot of the data. 
Therefore, I use the commands "plot(x,y)"to display the scatterplot;
then
"lines(x,fit)" to add the estimated regression line.
The graph displays the scatterplot correctly but not the fitted
regression line.
When I use the command "plot(x,fit)" I get the correct regression line
but the plot is very rough, it looks like that, instead of a single
clear line, I get several thin lines.

Hope this is clearer.

Thank you,

Chiara
Uwe Ligges wrote:
#
I guess you forgot that lines(x,y) , contrary to  plot(x,y)
needs  *increasing* (or decreasing) `x' to make sense in your
situation.

Hence use something like

plot(x,y)
fit <- yourcoolfunction(x,y, ...)
ix <- order(x)
lines(x[ix], fit[ix], col = 2)

HTH,
Martin
cp133> no, I'm afraid abline does not help, it works only
    cp133> for straight lines.  The output of my function is a
    cp133> vector of fitted values (called fit).  I would like
    cp133> to superimpose the fitted regression line to the
    cp133> scatterplot of the data.  Therefore, I use the
    cp133> commands "plot(x,y)"to display the scatterplot; then
    cp133> "lines(x,fit)" to add the estimated regression line.
    cp133> The graph displays the scatterplot correctly but not
    cp133> the fitted regression line.  When I use the command
    cp133> "plot(x,fit)" I get the correct regression line but
    cp133> the plot is very rough, it looks like that, instead
    cp133> of a single clear line, I get several thin lines.

    cp133> Hope this is clearer.

    cp133> Thank you,

    cp133> Chiara
cp133> Uwe Ligges wrote:
>>  cp133 wrote: > Hi,
    >> >
    >> > I have written my own function to estimate a local
    >> linear regression.  > When I plot the output vector on
    >> the scatterplot of the data, the > function lines does
    >> not work and I get a rescaled regression line.  > When I
    >> plot the output vector alone, the graph looks right, but
    >> choosing > the type "line" instead of the default does
    >> not deliver a clear single > line.
    >> >
    >> > Does somebody know why this is happening.
    >> >
    >> 
    >> Does abline() help?
    >> 
    >> If not, please be more specific in your question and tell
    >> us what you are exactly going to do.
    >> 
    >> Uwe Ligges
#
On Fri, 23 May 2003, cp133 wrote:

            
Please make sure the x are in increasing order, and the fit vector 
re-ordered to match, something like:

o <- order(x)
lines(x[o], fit[o])

I think the line is zigzagging backwards and forwards along the x vector.