[R] lm and time series.

Achim Zeileis Achim.Zeileis at wu-wien.ac.at
Sat Sep 6 09:53:35 CEST 2008


On Fri, 5 Sep 2008, rkevinburton at charter.net wrote:

> That is the thing. As a new comer to 'R' I don't understand how to write 
> a formula when all I have is a time series. I don't know how to express 
> the independent and dependent variables in a formula when the object is 
> a time series. So please just solve this simple example and I will 
> extrapolate from there.
>
> Say the units of the time series is days and the value at each point is 
> the response. If I wanted to fit a straiight line through the following 
> time series:
>
> y <- 4:7
> t <- ts(y)
>
> So this is saying to me something like 4 units were sold on the first 
> day, 5 on the second, 6 on the third, and 7 on the fourth.
>
> So given the time series t I want to find the slope and inercept:
>
> y = m*x + b
>
> with x in days and the respoinse would be the number of units sold. I 
> need to find 'm' and 'b'. If all I have is t (the time series above) 
> then what would be the formula, and for that matter the arguments to lm 
> to give the desired result?
>
> fit <- lm(???)

y <- ts(4:7)
lm(y ~ time(y))

And as previously pointed out to you: To preserve the time series 
properties, look at ?lm, the "dynlm" and "dyn" packages.
Z

> Thank you.
>
> Kevin
>
> ---- stephen sefick <ssefick at gmail.com> wrote:
>> So you want time as the independent variable?  Let's say that the
>> units of y in your first example were seconds- couldn't you just use a
>> regular lm and say that the units were seconds, minutes, or what ever?
>>  I am probably out of my league here, but I am just not understanding
>> what it is that you want.  a time series is just a series of data
>> points indexed by time.  Arima maybe, or some other cool times series
>> modeling approach- wavelet, spectral density- for frequency domain
>> type things...  What are you trying to accomplish?
>>
>> On Fri, Sep 5, 2008 at 5:47 PM,  <rkevinburton at charter.net> wrote:
>>> I want to fit a function to time series. If I had:
>>>
>>> x <- 1:4
>>> y <- 1:4
>>>
>>> lm(y~x)
>>>
>>> This would fit a simple line to the four points. But if it is represented as a time series
>>>
>>> x <- 1:4
>>> t <- ts(x)
>>>
>>> lm(????)
>>>
>>> So I have a time series in the object t. How do I write a formula for lm? What do I put in the formula for x and y when I only have t (the time series).
>>>
>>> Kevin
>>>
>>> ---- stephen sefick <ssefick at gmail.com> wrote:
>>>> what do you want to do?
>>>>
>>>> On Fri, Sep 5, 2008 at 3:22 PM,  <rkevinburton at charter.net> wrote:
>>>>> I am sorry but I looked at ?lm and could not see any guidance on writting a formula. If I have two arrays or a data set then I know how to do that (y ~ x) but for a time series I am not sure how to write y or x.
>>>>>
>>>>> Thank you.
>>>>>
>>>>> Kevin
>>>>>
>>>>> ---- Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
>>>>>> The Time Series section in ?lm should be self explanatory.   If you are using
>>>>>> diff's and lag's then look at the dyn package.
>>>>>>
>>>>>> On Fri, Sep 5, 2008 at 12:25 PM,  <rkevinburton at charter.net> wrote:
>>>>>>> I did a ?lm and it said basically to be careful when using lm and a time series. But my question is probably more to do with my inexperience that anything. If I have a time series object 'ti' how do I write the formula? The response is the value at any particular time and the time is basically the index of the time series. But I don't know how to put that into a formula.
>>>>>>>
>>>>>>> Thank you.
>>>>>>>
>>>>>>> Kevin
>>>>>
>>>>> ______________________________________________
>>>>> 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.
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Stephen Sefick
>>>> Research Scientist
>>>> Southeastern Natural Sciences Academy
>>>>
>>>> Let's not spend our time and resources thinking about things that are
>>>> so little or so large that all they really do for us is puff us up and
>>>> make us feel like gods. We are mammals, and have not exhausted the
>>>> annoying little problems of being mammals.
>>>>
>>>>       -K. Mullis
>>>
>>>
>>
>>
>>
>> --
>> Stephen Sefick
>> Research Scientist
>> Southeastern Natural Sciences Academy
>>
>> Let's not spend our time and resources thinking about things that are
>> so little or so large that all they really do for us is puff us up and
>> make us feel like gods. We are mammals, and have not exhausted the
>> annoying little problems of being mammals.
>>
>> 	-K. Mullis
>
> ______________________________________________
> 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