[R] question about "lines"

Duncan Murdoch murdoch.duncan at gmail.com
Wed Sep 18 13:20:26 CEST 2013


On 13-09-18 1:38 AM, meng wrote:
> Oh,yes, I found out this according to your reply.Thanks.
>
> As to time series analysis, in order to show the effect of smoothing or
> filtering,the common command is:
> plot(ts0);
> lines(fitted(...))
> But not "lines(fitted(...) ~ time(ts) )"
>
> How to understand this then?

lines() and plot() are "generic functions".  What it does depends on the 
class of the first argument.   To see what happens, you need to know the 
class of ts0, or fitted(...), or fitted(...) ~ time(ts).  I'd guess ts0 
has some time series class, fitted(...) probably has class "numeric" 
(though this would depend on the dots, since it is also generic), and 
the formula has class "formula".    "numeric" generally gets the default 
method (plot.default, lines.default); "formula" usually has its own 
methods (plot.formula, lines.formula), etc.  Read up on this in An 
Introduction to R for more details (sections 3.4 and 10.9).

Duncan Murdoch

>
> Many thanks.
>
> Best.
>
>
>
>
> At 2013-09-18 08:49:51,"Duncan Murdoch" <murdoch.duncan at gmail.com> wrote:
>>On 13-09-17 6:36 PM, meng wrote:
>>> Thanks for your reply.
>>>
>>> Is "fitted(lm(...))" the same as "values" of lines(values)?
>>>
>>> If yes,then why the range of lines(values) is different from
>>> range(fitted(lm(...)))?
>>
>>You are plotting against the wrong x axis, and you don't see all the values.
>>
>>Duncan Murdoch
>>
>>> If no, what "values" refers to?
>>>
>>>
>>>
>>> At 2013-09-17 20:56:04,"Duncan Murdoch" <murdoch.duncan at gmail.com> wrote:
>>>>On 13-09-17 8:06 AM, meng wrote:
>>>>> Hi all:
>>>>> I met a question about "lines".
>>>>>
>>>>>
>>>>> attach(cars)
>>>>>
>>>>>
>>>>> plot(dist ~ speed)
>>>>> #add the regression line to the plot
>>>>> lines(fitted(lm(dist~speed)) ~ speed)
>>>>>
>>>>>
>>>>> plot(dist ~ speed)
>>>>> #what kind of curve does the following command add to the plot?
>>>>> lines(fitted(lm(dist~speed)))
>>>>>
>>>>>
>>>>> My question is :
>>>>> what kind of curve does the last command add to the plot?
>>>>
>>>>Look at the class of fitted(lm(...)).  It is "numeric".  So what you're
>>>>seeing is the same as if you computed the fitted values, and then did
>>>>
>>>>lines(values)
>>>>
>>>>Since values is just a vector of numbers, that will plot them as y
>>>>values against x values 1:length(values).  That's unlikely to be a
>>>>useful thing to do.
>>>>
>>>>Duncan Murdoch
>>>>
>>>>>
>>>>>
>>>>> My guess:maybe the level of fitted values?
>>>>>> range(fitted(lm(dist~speed)))
>>>>> [1] -1.84946 80.73112
>>>>>
>>>>>
>>>>> But from the plot,I can see the range of the curve is about 10 to 40 more or less,which is different from(-1.84946, 80.73112).So the curve must not be the fitted values.What kind of curve does the last command add to the plot then?
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> Many thanks for your help
>>>>>
>>>>>
>>>>> My best
>>>>> 	[[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.
>>>>>
>>>>
>>>
>>>
>>>
>>
>
>
>



More information about the R-help mailing list