3
$\begingroup$

Let us say we have some demographic time series data which tells us how many hours people spend in front of a computer screen each day, grouped by age and gender:

set.seed(42)
dates = seq(as.Date("2011/1/1"), by="day", length.out = 100)
male.age1 = round(runif(100, min = 1, max = 10))
female.age1 = round(runif(100, min = 1, max = 10))
male.age2 = round(runif(100, min = 1, max = 10))
female.age2 = round(runif(100, min = 1, max = 10))
df = data.frame(dates = dates, male.age1 = male.age1, female.age1 = female.age1, male.age2 = male.age2, female.age2 = female.age2)

which looks like this:

> df[1:5,]
       dates male.age1 female.age1 male.age2 female.age2
1 2011-01-01         9           7         9           5
2 2011-01-02         9           3         6           5
3 2011-01-03         4           3         9           2
4 2011-01-04         8           5         5           4
5 2011-01-05         7           9         2           9
etc...

I am just wondering what kind of analysis would be appropriate for this type of data, and if you could give me some examples? My initial thinking is trying to see how certain types of people act on certain dates, though I don't know how to go about that. I think what I'm finding hard is that the dates are not really independent of each other (i.e. what happens today might effect what happens tomorrow).

Many thanks in advance for your time.

Note: This isn't homework or anything like that, just curiosity and trying to learn R.

Note 2: Is there any benefit in reshaping the data as follows (I came across the reshape package a few days ago, and it looks really cool):

library(reshape)
library(reshape2)
y = melt(df, id = "dates")
y$gender = NA
ind = grep("female", y$variable, fixed = TRUE)
y[ind, "gender"] = "female"
y[-ind, "gender"] = "male"
y$gender = factor(y$gender)
levels(y$variable) <- c("age1", "age2", "age3", "age4")

Which gives:

> y[with(y, order(dates)), ][1:10,]
         dates variable value gender
1   2011-01-01     age1     9   male
101 2011-01-01     age2     7 female
201 2011-01-01     age3     9   male
301 2011-01-01     age4     5 female
2   2011-01-02     age1     9   male
102 2011-01-02     age2     3 female
202 2011-01-02     age3     6   male
302 2011-01-02     age4     5 female
3   2011-01-03     age1     4   male
103 2011-01-03     age2     3 female
    etc...
$\endgroup$

1 Answer 1

2
$\begingroup$

If you're trying to learn time series in R, I would suggest you to use real data and not simulated data.

This is so because in time series there many effects due to time, such as seasonality and trend.

I would suggest you to take a look at

?ts
?ts.plot
?decompose
?arima

If you want to study this simulated data sets, you may find useful grouping your data as time series using male.age1, female.age1, male.age2, female.age2. For example

m.age1.series <- ts( male.age1, start=c(01,01) , frequency=30 )

This will create a time series object that you may analyze.

Take a look at the following link: http://www.stat.pitt.edu/stoffer/tsa2/R_time_series_quick_fix.htm

I hope it helps! :)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.