Skip to content
Prev 304365 / 398503 Next

date in plot, can't add regression line

Hello,

Inline.
Em 28-08-2012 18:23, Nordlund, Dan (DSHS/RDA) escreveu:
It does, I've just tried it.

Also, regarding the op statement "(I'm sorry it's not in a runnable 
form, not sure how to get that from R)":

# It's easy to read in the data
tfday <- read.table(text="
    tradeflavor  timestamp   x
1              1 2009-01-22   1
2              2 2009-01-22   1
3              1 2009-01-23   1
[...etc...]
39             1 2009-02-12 153
40             2 2009-02-12   6
", header=TRUE, stringsAsFactors=FALSE)

# But it's better to paste the output of dput().
dput(tfday)
structure(list(tradeflavor = c(1L, 2L, 1L, 1L, 1L, 2L, 16L, 1L,
16L, 1L, 1L, 16L, 1L, 4L, 8L, 1L, 8L, 16L, 1L, 8L, 16L, 1L, 4L,
8L, 16L, 1L, 2L, 4L, 8L, 1L, 4L, 8L, 16L, 1L, 2L, 4L, 8L, 16L,
1L, 2L), timestamp = structure(c(1232582400, 1232582400, 1232668800,
1233014400, 1233100800, 1233100800, 1233100800, 1233187200, 1233187200,
1233273600, 1233532800, 1233532800, 1233619200, 1233619200, 1233619200,
1233705600, 1233705600, 1233705600, 1233792000, 1233792000, 1233792000,
1233878400, 1233878400, 1233878400, 1233878400, 1234137600, 1234137600,
1234137600, 1234137600, 1234224000, 1234224000, 1234224000, 1234224000,
1234310400, 1234310400, 1234310400, 1234310400, 1234310400, 1234396800,
1234396800), class = c("POSIXct", "POSIXt"), tzone = ""), x = c(1L,
1L, 1L, 54L, 105L, 2L, 2L, 71L, 2L, 42L, 19L, 2L, 36L, 2L, 3L,
73L, 12L, 7L, 53L, 6L, 9L, 38L, 6L, 2L, 3L, 42L, 2L, 1L, 2L,
87L, 2L, 4L, 3L, 55L, 6L, 4L, 2L, 8L, 153L, 6L)), .Names = c("tradeflavor",
"timestamp", "x"), row.names = c("1", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17",
"18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28",
"29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39",
"40"), class = "data.frame")

# Now all we need to do is copy and paste this into an R session:

tfday <- structure(...etc...)

# Finally, for the sake of completeness, the rest of the code.
tfday$timestamp <- as.POSIXct(tfday$timestamp)
inx <- tfday$tradeflavor == 1 # do this once
xdates <- tfday$timestamp[inx]
ydata <- tfday$x[inx]

plot(xdates, ydata)
model <- lm(ydata ~ xdates)
abline(model)

Hope this helps,

Rui Barradas