How to define date column and plot that date column as x axis in R studio - date

I have half-hourly time series data; 48 data points in a day. The total time period starts from 4/6/2018 (i.e day/month/year) to 2/12/2018. I want to create a time series object. I am using ts function.
However, I am unable to define the frequency. As I know, frequency=1 for yearly data, but unable to define in my half-hourly data. How to solve this problem using ts object or there is any other way to do it?
After creating a time series object (i.e. date column), I want to plot it with the corresponding data series.
How to plot my result (in R object) on Y-axis and time period on the X-axis?
R <- function(x){
return(FDWhittle(x, method="discrete", sdf.method="multitaper"))
}
plot(R[,i],type="l",col = "Black", xlab="Time",
ylab="Return",main=names(R)[,i])
When I am plotting by using the above code, the results in the object "R" is coming on the Y-axis, and some values like (100, 200, 300--------) are coming on the X-axis. I want to get the time period on my X axis, but unable to define the intra-day date column having 48 data points in a day.

Related

How do I take an n-day average of data in Matlab to match another time series?

I have daily time series data and I want to calculate 5-day averages of that data while also retrieving the corresponding start date for each of the 5-day averages. For example:
x = [732099 732100 732101 732102 732103 732104 732105 732106 732107 732108];
y= [1 5 3 4 6 2 3 5 6 8];
Where x and y are actually size 92x1.
Firstly, how do I compute the 5-day mean when this time series data is not divisible by 5? Ultimately, I want to compute the 'jumping mean', where the average is not computed continuously (e.g., June 1-5, June 6-10, and so on).
I've tried doing the following:
Pentad_avg = mean(reshape(y(1:90),5,[]))'; %manually adjusted to be divisible by 5
Pentad_dt = x(1:5:90); %select every 5th day for time
However, Pentad_dt gives me dates 01-Jun-2004 and 06-Jun-2004 as output. And, that brings me to my second point.
I am looking to find 5-day averages for x and y that correspond to 5-day averages of another time series. This second time series has 5-day averaged data starting from 15-Jun-2004 until 29-Aug-2004 (instead of starting at 01-Jun-2004). Ultimately, how do I align the dates and 5-day averages between these two time series?
Synchronization between two time series can be accomplished using the timeseries object. Placing your data into an object allows Matlab to intelligently process it. The most useful thing is adds for your usage is the synchronize method.
You'll want to make sure to properly set the time vector on each of the timeseries objects.
An example of what this might look like is as follows:
ts1 = timeseries(y,datestr(x));
ts2 = timeseries(OtherData,OtherTimes);
[ts1 ts2] = synchronize(ts1,ts2,'Uniform','Interval',5);
This should return to you each timeseries aligned to be with the same times. You could also specify a specific time vector to align a timeseries to using the resample method.

How to draw time based graphs using ios-charts

I'm trying to draw a temperature graph using iso-charts where the x axis data would be set from a server timestamp but the labels would be readable text.
For instance the graph x-axis label would start at Monday 00:00 and end Tuesday 12pm but the LineChartDataSet would be a collection of temperature (y-axis) and timestamps for the x
To display the timestamp I have a custom valueFormatter set as follow (which works great)
lineChartView.xAxis.valueFormatter = timestampXAxisFormatter() //converts timestamp to Date string
My question: The LineChartDataSet seems to be indexed based which is causing some trouble: if I have 4 data points such as (9am, 10), (9:15am, 11), (12pm, 15), (1pm, 16) the 4 points are set in the chart at regular intervals (I was expecting 2 points to be on the left side of the graph and then last 2 points on the right side) - Is there a way to have a data set that is based on the x value instead of the index?
I saw ChartData has an init that takes an array of NSObjects but then it converts it to Strings...Thanks in advance for any suggestions you may have!
There is no good way to solve it, as you figured out the x axis is index based.
You have two options:
insert many x values between each real x value, like between 9:00 and 9:15, you manually insert 9:01, 9:02, ..., 9:14, but don't add any entry at these values, just ignore it and continue. ios-charts will skip if no entry found and go to next. This will works fine, if you don't have a large number of values to insert. I tried ~1000 values, the performance is acceptable.
you create your own chart, using two y axis, one as x axis and one as y axis, so the distances to 0 point are calculated by value. However this requires you understand the ios-chart logic deeply. If you succeed, you are more than welcome to file a PR.

Unexpected date when ploting a timeseries MATLAB

I am trying to plot a timeseries using a cell array of strings representing an hour of measurements with a sample every 10 seconds. Below is the code I use to plot this data:
Voltages=[230.1,235.1,.......237];
Time={'13:00:10','13:00:20', '13:00:30'........'14:00:00'};
t=timeseries(Voltages, Time); % Using timeseries function in MATLAB
plot(t);
I also add two straight lines showing upper and lower voltage limits, and here is the chart I get:
As you can see in final result an unexpected date is shown on the x-axis ...
I want to do one of the following:
Remove the date altogether from the x-axis.
Using a date string 06.05.2015 I have in a variable, add this date instead of the 1-Jan-2015 unexpected date.
Remove the unexpected date from x-axis and then add the 06.05.2015 in text box.
From the documentation of timeseries class:
ts = timeseries(data,time) creates the time-series object using the specified data and time.
Where time above is a "time vector" as defined further down on the same documentation page:
Time Vector
A time vector of a timeseries object can be either numerical (double) values or valid MATLAB date strings.
When the timeseries TimeInfo.StartDate property is empty, the numerical time values are measured relative to 0 (or another numerical value) in specified units. In this case, the time vector is described as relative (that is, it contains time values that are not associated with a specific start date).
Before plotting the timeseries, try setting the TimeInfo.StartDate to what you need it to be, as shown in the example below:
Time = ['13:00:10';'13:00:20';'13:00:30'];
Data = [1 2.5 3];
start_date = '06.05.2015'; %// What you specified
ts = timeseries(Data,Time);
%// Since a date format separated with dots isn't supported in MATLAB, we replace . => /
ts.TimeInfo.StartDate = strrep(start_date,'.','/');
plot(ts)
Which results in:
                   

MATLAB Display real-time dates on x-axis

I created a diagram presenting limb movement over the time (2.56 seconds). My diagram looks like in the top Picture 1.
My code to get the diagram was:
x=data(1000:1256,2)
Fs=100
Ts=1/Fs
L=length(x)
t = (0:L-1)*Ts;
figure
plot(t,x);
Now Im trying to change time units into real time data (day and time, when the measurements were recorded), I want to get something like in the Picture 2: mark x-axis with date and time. Ideally Id like to have 6 time marks.
Using:
datestr(data(1000),'dd-mm-yyyy HH:MM:SS AM')
I know the first time (row 1000th of my data) is 10-07-2010 11:31:50 PM and the last row (1256th) is 10-07-2010 11:43:42 PM. There was always 50 records recorded per 1 second, BUT the problem is, the measurements were not recorded constantly - I mean, sometimes there was no measurements for a few minutes (when there was no movement).
I ve been trying to use XTick etc but I dont know how to select the real-time data for my x-axis and how to label x-axis with the real time of measurements.
labels=datestr(data);
set(gca,'XTick',1:6; 'XTickLabel',labels);
Anybody could help me?
This should format the tickmarks
datetick('x', 'dd-mm-yyyy HH:MM:SS AM')
It might go after setting the tickmarks
set(gca,'XTick',[ ... ])
Where in the array you should put the datenum values of your times.
Consider this minimal example:
x = linspace(now-2, now, 30);
y = rand(30, 1);
plot(x, y)
datetick('x', 'dd-mmm HH:MM')

labeling x-axis with cell array

I have been searching various forums for hours but it seems impossible to do a thing in Matlab that's automatic in excel...
I used uiimport to import an xls file with into two arrays (? total newbie), one containing dates for my x-axis and the other the values I want to plot.
I have 180 values. The dates are three dates per month, more or less ranging from May 2008 until now, end of March.
Using
plot(mynumbers)
set(gca,'XTickLabel',dates)
only puts dates for May 2008 on my x-axis!
where did all the other dates go?
Instead using
plot(mynumbers)
set(gca,'XTick',mynumbers,'XTickLabel',dates)
gives error message
"??? Error using ==> set
Values must be monotonically increasing."
Please help!
where did all the other dates go?
The answer to your first question is that MATLAB only uses the first N number of strings corresponding to the default N number of tick marks on the x axis.
"??? Error using ==> set Values must be monotonically increasing."
The error is telling you that your date ticks must be evenly spaced. Instead of using dates corresponding to your actual data points, you could grab the x tick values that MATLAB automatically assigned to your graph, translate them to text, and then reassign the dates as x tick labels, like so:
% generate example unevenly spaced date vector
time = [now,now+1,now+25,now+28.5,now+36,now+40,now+51,now+65];
% generate random data points
data = rand(size(time));
% plot time vs data, storing the axes handle in the process
figure;
axH = axes;
plot(axH,time,data)
% get the x-axis tick locations
ticLoc = get(axH,'XTick');
% format tick labels (substitute any date format you wish)
ticLab = cellfun(#(x) datestr(x,'mm/dd'),num2cell(ticLoc),'UniformOutput',false);
% apply tick labels
set(axH,'XTickLabel',ticLab)
MATLAB's built-in function datetick also performs similarly.
However, if you zoom afterwards, you won't have accurate tick labels. So you may want to use datetick2 on the File Exchange.
If you're having trouble converting a cell array of dates from Excel into a numeric array, use:
dateNumeric = cell2mat(cellfun(#datenum,dateStrings,'UniformOutput',false));
try set (gca,'XTickLabel',num2str(dates))