[R] LM() and time in R

Jim Lemon drjimlemon at gmail.com
Thu Apr 30 10:33:47 CEST 2015


Hi Livia,
You can convert the time of day fields using strptime like this:

strptime(sapply(strsplit("time: 08:14:22"," "),"[",2),"%H:%M:%S")

This will give you the times as seconds in the current day. If you
then wish to divide the day into morning, afternoon and night, you
could create three time cutpoints, say:

strptime("06:00:00","%H:%M:%S")
strptime("12:00:00","%H:%M:%S")
strptime("20:00:00","%H:%M:%S")

such that any time less than the first or more than the third would be
night, and so on. I think it might be a good idea to factor the daily
price of crude oil (get this from the date) into the regression just
to see how much variance that explains compared to time of day
variations.

Jim

On Thu, Apr 30, 2015 at 6:06 PM, Livia Maria Vestergaard
<lvest09 at student.sdu.dk> wrote:
> Hello guys,
> thanks for your quick reply
> I will try and specific my problem
>
> I have over 300.000 observations for different petrol pumps in all of Denmark fra 1. november 2014 - 31 january 2015
> for example I have:
>
> petrol pump number:  "1111" /  date: 1. november 2014 / time: 08:14:22 / price: 10.55 /day /   zipcode: 2000
> petrol pump number:  "1111" /  date: 1. november 2014 / time: 08:29:00 / price: 10.52 / day / zipcode: 2000
> petrol pump number: "3456" / date: 1. november 2014 / time: 08:19:21 / price: 10.88 / day/  zipcode: 2100
>
>
> And then put the zip codes into dummy categories like X1: zipcode (1000-2000)=1 all others 0 and so on.
>
> My regression model then look like
>
> lm(price, x1 + x2 + x3 ...  ) but the "Multiple R-squared" is really low, 0.01217
>
> So instead I would like to check if the time "hh:mm:ss" have an effect on the price. I know how to tell R about the date by as.Date() , but not with the time, how can I do that?
>
>
> Or do you guys maybe a third idea about what should make a regression about to improve the model?
>
> Cheers
> Livia :)
>
> ________________________________________
> Fra: Boris Steipe [boris.steipe at utoronto.ca]
> Sendt: 29. april 2015 20:25
> Til: Livia Maria Vestergaard
> Cc: r-help mailing list
> Emne: Re: [R] LM() and time in R
>
> Have a look at the help page for the function ts()
> (type ?ts  at the R prompt).  Other than that, you haven't provided nearly enough information for us to diagnose your issue.
>
> Please see here for some hints on how to ask questions productively:
> http://adv-r.had.co.nz/Reproducibility.html
> http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example
> Cheers
>
> B.
>
> On Apr 29, 2015, at 6:26 AM, Livia Maria Vestergaard <lvest09 at student.sdu.dk> wrote:
>
>>
>> Hello,
>>
>> I need some help with a project that I’m working one.
>>
>> Im trying to make a l regression model (lm) in r with time as independant variable and gas prices as the depended.
>> But It seems like everything im trying to run it, R freeze, I think that I need to tell R somehow that my time is time but how ?
>>
>>
>>
>> Hope that you can help me :)
>> Livia
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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