#long data frame
#---------------------
data("fagus_dynamics")
#name_column is name
#time column is time
str(fagus_dynamics)
#to tsl
#each group in name_column is a different time series
tsl <- tsl_initialize(
x = fagus_dynamics,
name_column = "name",
time_column = "time"
)
#check validity (no messages or errors if valid)
tsl_diagnose(tsl)
#class of contained objects
lapply(X = tsl, FUN = class)
#get list and zoo names (between double quotes)
tsl_names_get(
tsl = tsl,
zoo = TRUE
)
#plot tsl
if(interactive()){
tsl_plot(tsl)
}
#list of zoo objects
#--------------------
x <- zoo_simulate()
y <- zoo_simulate()
tsl <- tsl_initialize(
x = list(
x = x,
y = y
)
)
#plot
if(interactive()){
tsl_plot(tsl)
}
#wide data frame
#--------------------
#wide data frame
#each column is same variable in different places
df <- stats::reshape(
data = fagus_dynamics[, c(
"name",
"time",
"evi"
)],
timevar = "name",
idvar = "time",
direction = "wide",
sep = "_"
)
str(df)
#to tsl
#key assumptions:
#all columns but "time" represent
#the same variable in different places
#all time series are of the same length
tsl <- tsl_initialize(
x = df,
time_column = "time"
)
#colnames are forced to be the same...
tsl_colnames_get(tsl)
#...but can be changed
tsl <- tsl_colnames_set(
tsl = tsl,
names = "evi"
)
tsl_colnames_get(tsl)
#plot
if(interactive()){
tsl_plot(tsl)
}
#list of vectors
#---------------------
#create list of vectors
vector_list <- list(
a = cumsum(stats::rnorm(n = 50)),
b = cumsum(stats::rnorm(n = 70)),
c = cumsum(stats::rnorm(n = 20))
)
#to tsl
#key assumptions:
#all vectors represent the same variable
#in different places
#time series can be of different lengths
#no time column, integer indices are used as time
tsl <- tsl_initialize(
x = vector_list
)
#plot
if(interactive()){
tsl_plot(tsl)
}
#list of matrices
#-------------------------
#create list of matrices
matrix_list <- list(
a = matrix(runif(30), nrow = 10, ncol = 3),
b = matrix(runif(80), nrow = 20, ncol = 4)
)
#to tsl
#key assumptions:
#each matrix represents a multivariate time series
#in a different place
#all multivariate time series have the same columns
#no time column, integer indices are used as time
tsl <- tsl_initialize(
x = matrix_list
)
#check column names
tsl_colnames_get(tsl = tsl)
#remove exclusive column
tsl <- tsl_subset(
tsl = tsl,
shared_cols = TRUE
)
tsl_colnames_get(tsl = tsl)
#plot
if(interactive()){
tsl_plot(tsl)
}
#list of zoo objects
#-------------------------
zoo_list <- list(
a = zoo_simulate(),
b = zoo_simulate()
)
#looks like a time series list! But...
tsl_diagnose(tsl = zoo_list)
#let's set the names
zoo_list <- tsl_names_set(tsl = zoo_list)
#check again: it's now a valid time series list
tsl_diagnose(tsl = zoo_list)
#to do all this in one go:
tsl <- tsl_initialize(
x = list(
a = zoo_simulate(),
b = zoo_simulate()
)
)
#plot
if(interactive()){
tsl_plot(tsl)
}
Run the code above in your browser using DataLab