library(data.table)
var = fread("Q:\\Electricity\\Analysis\\6 Working\\NZ Power\\Jack Perry\\Rainfall\\Clutha R (V01).csv")
var.ts = ts(var$Rainfall, start = c(2008,5),end = c(2020), frequency = 53)
n = length(var.ts)
n
print(var.ts)
plot(var.ts);
var.decomp = stl(var.ts,s.window = 'periodic', t.window = 500)
plot(var.decomp)
Hi,
I need help pulling the data from a timeseries decomposition. I want to take the data from the seasonal component (not just look at the plot).
Thanks,
Jack
The structure is a list of elements. So, we extract the 'time.series' component and get the 'seasonal' column
var.decomp$time.series[, "seasonal"]
Related
I'm trying to up the number of starting values in Mplus when I pass objects through the program using the mplusModeler package in R, specifically the mplusObject function. Is there a way to specify the number of starts (e.g., 100 10) instead of the defaults? Or is this something I can only do in Mplus and there's no way to do this from R?
I figured it out!
You add the command to the ANALYSIS section of the input. For example:
lpa_model <- mplusObject(
TITLE = "3-Class LPA;",
VARIABLE = "USEVARIABLES = x01-x15;
CLASSES=c(3);",
ANALYSIS = "ESTIMATOR = MLR;
TYPE = MIXTURE;
STARTS=100 10;",
MODEL = " [code continues...]
I have data like the following (three categorical values):
data <- data.frame(Comp = (rep(c('Oral','Text'), each = 8)),
Cat = rep(c('Declative','Non-declative'), 4),
Type = rep(c('Free','Used'), each = 4))
I want to have an interaction.plot or barplot with these three categorical values in R. Would you give me any tip?
Although your supplied data don't produce a good display since the cells are all equal sized you can use base r to make a mosaic plot.
data <- data.frame(Comp = (rep(c('Oral','Text'), each = 8)),
Cat = rep(c('Declative','Non-declative'), 4),
Type = rep(c('Free','Used'), each = 3))
mosaicplot(table( data$Comp,data$Cat,data$Type))
Here's a variant on your data that shows it a little better.
data <- data.frame(Comp = (rep(c('Oral','Text'), each = 8)),
Cat = rep(c('Declative','Non-declative'), 4),
Type = c(rep(c('Free','Used'), each = 3), c('Used', 'Used')))
mosaicplot(table( data$Comp,data$Cat, data$Type))
Of course you can go to specialized packages to get other variations, vcd is one but if you search you will find others.
I was reading through this blog post on R-bloggers and I'm confused by the last section of the code and can't figure it out.
http://www.r-bloggers.com/self-organising-maps-for-customer-segmentation-using-r/
I've attempted to recreate this with my own data. I have 5 variables that follow an exponential distribution with 2755 points.
I am fine with and can plot the map that it generates:
plot(som_model, type="codes")
The section of the code I don't understand is the:
var <- 1
var_unscaled <- aggregate(as.numeric(training[,var]),by=list(som_model$unit.classif),FUN = mean, simplify=TRUE)[,2]
plot(som_model, type = "property", property=var_unscaled, main = names(training)[var], palette.name=coolBlueHotRed)
As I understand it, this section of the code is suppose to be plotting one of the variables over the map to see what it looks like but this is where I run into problems. When I run this section of the code I get the warning:
Warning message:
In bgcolors[!is.na(showcolors)] <- bgcol[showcolors[!is.na(showcolors)]] :
number of items to replace is not a multiple of replacement length
and it produces the plot:
Which just some how doesn't look right...
Now what I think it has come down to is the way the aggregate function has re-ordered the data. The length of var_unscaled is 789 and the length of som_model$data, training[,var] and unit.classif are all of length 2755. I tried plotting the aggregated data, the result was no warning but an unintelligible graph (as expected).
Now I think it has done this because unit.classif has a lot of repeated numbers inside it and that's why it has reduced in size.
The question is, do I worry about the warning? Is it producing an accurate graph? What exactly is the "Property"'s section looking for in the plot command? Is there a different way I could "Aggregate" the data?
I think that you have to create the palette color. If you put the argument
coolBlueHotRed <- function(n, alpha = 1) {rainbow(n, end=4/6, alpha=alpha)[n:1]}
and then try to get a plot, for example
plot(som_model, type = "count", palette.name = coolBlueHotRed)
the end is succesful.
This link can help you: http://rgm3.lab.nig.ac.jp/RGM/R_rdfile?f=kohonen/man/plot.kohonen.Rd&d=R_CC
I think that not all of the cells on your map have points inside.
You have 30 by 30 map and about 2700 points. In average it's about 3 points per cell. With high probability some cells have more than 3 points and some cells are empty.
The code in the post on R-bloggers works well when all of the cells have points inside.
To make it work on your data try change this part:
var <- 1
var_unscaled <- aggregate(as.numeric(training[, var]), by = list(som_model$unit.classif), FUN = mean, simplify = TRUE)[, 2]
plot(som_model, type = "property", property = var_unscaled, main = names(training)[var], palette.name = coolBlueHotRed)
with this one:
var <- 1
var_unscaled <- aggregate(as.numeric(data.temp[, data.classes][, var]),
by = list(som_model$unit.classif),
FUN = mean,
simplify = T)
v_u <- rep(0, max(var_unscaled$Group.1))
v_u[var_unscaled$Group.1] <- var_unscaled$x
plot(som_model,
type = "property",
property = v_u,
main = colnames(data.temp[, data.classes])[var],
palette.name = coolBlueHotRed)
Hope it helps.
Just add these functions to your script:
coolBlueHotRed <- function(n, alpha = 1) {rainbow(n, end=4/6, alpha=alpha)[n:1]}
pretty_palette <- c("#1f77b4","#ff7f0e","#2ca02c", "#d62728","#9467bd","#8c564b","#e377c2")
As you know, Our country is famous for the popular music industry, named "K-POP".
Recently, the 'tuneR' package to analyze 'wav.file' was released in R.
So I want to know any tendency of the K-POP musics and cluster them by using this package.
In analysis, I got a graph to show the notes of a music file (see example below)
My problem is that I don't know how can I extract the peak value's notes from this graph (those circled in blue)
Please let me know some methods if you know any functions to resolve this problem in R.
library("tuneR")
transcribeMusic <- function(wavFile, widthSample = 4096, expNotes = NULL)
{
perioWav <- periodogram(wavFile, width = widthSample)
freqWav <- FF(perioWav)
noteWav <- noteFromFF(freqWav)
melodyplot(perioWav, observed = noteWav, expected = expNotes, plotenergy = FALSE)
}
songHlp <- readMP3("Beenzino.mp3")
testSound_stereo <- extractWave(songHlp, from = 0, to = 12, xunit = "time")
testSound <- mono(testSound_stereo, "both")
transcribeMusic(testSound)
I have time series data on 10 individuals, similar to what is created below. I am looking for a way to make a 3-D plot similar to this example:
Except I want each unique ID to be where year is in the picture
set.seed(123)
ID <- rep(1:10, each = 500)
Time = rep(c(1:500),10)
Var <- rnorm(5000)
data <- data.frame(
ID = factor(ID),
Time = Time,
Variable = Var
)
As a very quick, and ugly start, try
library(plot3D)
## rearrange data into matrix form
m <- matrix(
data$Variable,
nrow=length(unique(data$ID)))
hist3D(z = m)
This doesn't look at all like your example plot; on the other hand, your data don't look much like the data in this plot. Things I haven't played around with yet:
axis labels
making sure the matrix is actually oriented in the right way (this will be a lot easier with real data!)
changing aspect ratio