X before the date in xts time series - time-series

I have these 2 columns in 1 column I have the date and time and the other column reading of the device.
I am using xts to convert the time series into xts object but after converting I am not able to plot the time series and I am getting x before the date. and the error 'x' must be a time-series object. i have even used as.POSIXct(x, format = "%d.%m.%Y %H:%M:%OS") but still I am getting an error.
X2020.10.13.09.54.00
0.165
X2020.10.13.09.54.30
0.166
X2020.10.13.09.55.00
0.166
X2020.10.13.09.55.30
0.166
X2020.10.13.09.56.00
0.166
X2020.10.13.09.56.30
0.166
X2020.10.13.09.57.00
0.166
X2020.10.13.09.57.30
0.166
X2020.10.13.09.58.00
0.166
X2020.10.13.09.58.30
0.166
X2020.10.13.09.59.00
0.166

The problem is solved. I have figured it out. I still have X in my View data but the error ' the error 'x' must be a time-series object' is gone. The problem was my numbers were in character form. So, I have converted them to numeric form and now I am able to have time series and plots.

Related

Time series in R: How to convert raw data from int type to time type

For subject 1 in the training data, I am trying to plot nine time series corresponding to nine different features. The data is supposed to be a time series but R is not reading it as such. The first two columns, as you can see, are not time but the rest should be. How do I do this in R or Rmarkdown (I think they should be the same)?
I tried plotting it:
ggplot(train_ds, aes(x = Activity, y = TimeBodyAccelerometer-mean-X) +
theme_minimal() +
geom_point()
)
but I get this error:
Error in ggplot():
! mapping should be created with aes().
✖ You've supplied a object
Backtrace:
ggplot2::ggplot(...)
ggplot2:::ggplot.default(...)
Error in ggplot(train_ds, aes(x = Activity, y = TimeBodyAccelerometer - :
✖ You've supplied a object

Parse time string to hours, minutes and seconds in Lua

I am currently working on a plugin for grandMA2 lighting control using Lua. I need the current time. The only way to get the current time is the following function:
gma.show.getvar('TIME')
which always returns the current system time, which I then store in a variable. An example return value is "12h54m47.517s".
How can I separate the hours, minutes and seconds into 3 variables?
If os.date is available (and matches gma.show.getvar('TIME')), this is trivial:
If format starts with '!', then the date is formatted in Coordinated Universal Time. After this optional character, if format is the string "*t", then date returns a table with the following fields: year, month (1–12), day (1–31), hour (0–23), min (0–59), sec (0–61, due to leap seconds), wday (weekday, 1–7, Sunday is 1), yday (day of the year, 1–366), and isdst (daylight saving flag, a boolean). This last field may be absent if the information is not available.
local time = os.date('*t')
local hour, min, sec = time.hour, time.min, time.sec
This does not provide you with a sub-second precision though.
Otherwise, parsing the time string is a typical task for tostring and string.match:
local hour, min, sec = gma.show.getvar('TIME'):match('^(%d+)h(%d+)m(%d*%.?%d*)s$')
-- This is usually not needed as Lua will just coerce strings to numbers
-- as soon as you start doing arithmetic on them;
-- it still is good practice to convert the variables to the proper type though
-- (and starts being relevant when you compare them, use them as table keys or call strict functions that check their argument types on them)
hour, min, sec = tonumber(hour), tonumber(min), tonumber(sec)
Pattern explanation:
^ and $ pattern anchors: Match the full string (and not just part of it), making the match fail if the string does not have the right format.
(%d)+h: Capture hours: One or more digits followed by a literal h
(%d)+m: Capture minutes: One or more digits followed by a literal m
(%d*%.?%d*)s: Capture seconds: Zero or more digits followed by an optional dot followed by again zero or more digits, finally ending with a literal s. I do not know the specifics of the format and whether something like .1s, 1.s or 1s is occasionally emitted, but Lua's tonumber supports all of these so there should be no issue. Note that this is slightly overly permissive: It will also match . (just a dot) and an s without any leading digits. You might want (%d+%.?%d+)s instead to force digits appearing before & after the dot.
Lets do it with string method gsub()
local ts = gma.show.getvar('TIME')
local hours = ts:gsub('h.*', '')
local mins = ts:gsub('.*%f[^h]', ''):gsub('%f[m].*', '')
local secs = ts:gsub('.*%f[^m]', ''):gsub('%f[s].*', '')
To make a Timestring i suggest string method format()
-- secs as float
timestring = ('[%s:%s:%.3f]'):format(hours, mins, secs)
-- secs not as float
timestring = ('[%s:%s:%.f]'):format(hours, mins, secs)

how to automatically detect and correct an abnormal period of data in a time serie using python

I am trying to find a method that allows me to automatically detect and correct abnormal periods of data points in a time series (a sequence of outliers). I've already tried ThymeBoost but it only corrects point outliers and not outlier periods.
Here is an example of a time series containing a period of outliers
the time series :
01/02/2018 288.000000
01/03/2018 332.000000
01/04/2018 277.000000
01/05/2018 233.000000
01/06/2018 204.000000
01/07/2018 216.000000
01/08/2018 175.000000
01/09/2018 218.000000
01/10/2018 413.000000
01/11/2018 416.000000
01/12/2018 151.000000
01/01/2019 224.000000
01/02/2019 563.000000
01/03/2019 413.000000
01/04/2019 238.000000
01/05/2019 343.000000
01/06/2019 176.000000
01/07/2019 533.103060
01/08/2019 230.000000
01/09/2019 364.000000
01/10/2019 324.000000
01/11/2019 437.000000
01/12/2019 738.000000
01/01/2020 619.000000
01/02/2020 728.000000
01/03/2020 506.000000
01/04/2020 500.000000
01/05/2020 886.000000
01/06/2020 892.000000
01/07/2020 268.000000
01/08/2020 32.000000
01/09/2020 45.000000
01/10/2020 51.000000
01/11/2020 373.000000
01/12/2020 61.000000
01/01/2021 73.000000
01/02/2021 779.000000
01/03/2021 584.718872
01/04/2021 614.000000
01/05/2021 489.000000
01/06/2021 534.000000
01/07/2021 455.000000
The plot:
I have also tried to use the seasonal decompose but it doesn't work since the series doesn't seem to have a seasonality

SPSS Compute a new variable with the last four digits of a numeric variable

I have a numeric variable (year of birth) in SPSS and i would like to take the last for digits out of it. Most values are like 1988, 2001, 1948 etc. But about 250 respondents entered their year of birth like 30-2-1947, or 2-9-1984 etc. That means not all values have the same length. By taking the last 4 digits into a new variable I could create an age category for all the respondents.
How can I do that?
I tried by converting the variable to a string and using substr to get a part of the value, but I always had to choose a starting point. I want to start from the last digit and then move backwards.
Instead of using SUBSTR() you can try using RIGHT() to grab the last four digits.
* convert yob to string variable.
ALTER TYPE yob (A10).
EXE .
* use RIGHT to extract the last 4 digits and convert to numeric.
COMPUTE n_yob = NUMBER(RIGHT(yob, 4, F4)) .
EXE .
You can now use n_yob to calcuate age (ex: COMPUTE age = 2022-n_yob .). You can also use ALTER_TYPE again if you want to convert yob back to it's original type.

Cobol not numeric name

I am having some trouble with some cobol code. In the following code bellow it causes these two errors. The compiler does not like when I try and do the addition.
CH7PPB.CBL:158: Error: 'NEW-DUES' is not numeric name
CH7PPB.CBL:161: Error: 'NEW-INSURANCE' is not numeric name
MOVE UNION-DUES TO OLD-DUES
MULTIPLY UNION-DUES BY .04 GIVING NEW-DUES
ADD UNION-DUES TO NEW-DUES
MOVE INSURANCE TO OLD-INSURANCE
MULTIPLY INSURANCE BY .03 GIVING NEW-INSURANCE
ADD INSURANCE TO NEW-INSURANCE
NEW-DUES and NEW-INSURANCE are defined as follows.
05 NEW-DUES PIC Z9(4).99.
05 NEW-INSURANCE PIC Z9(4).99.
Thank you for any help.
I'm glad you already solved the issue.
Just for the record, you cannot use edited formats in computations. The character "Z" in the PICTURE string of both variables turns the variable to be not considered numeric by the compiler.
You indeed have to declare a full numeric variable (no formatting) and move its result value to the formatted variable after.
Resolved this by useing two temp variables not sure if this is the way to do it but it worked.
MOVE ANNUAL-SALARY TO OLD-SALARY
MULTIPLY ANNUAL-SALARY BY .07 GIVING TEMP
ADD ANNUAL-SALARY,TEMP TO TEMP2
MOVE TEMP2 TO NEW-SALARY
MOVE ZEROS TO TEMP, TEMP2
MOVE UNION-DUES TO OLD-DUES
MULTIPLY UNION-DUES BY .04 GIVING TEMP
ADD UNION-DUES,TEMP TO TEMP2
MOVE TEMP2 TO NEW-DUES
MOVE ZEROS TO TEMP, TEMP2
MOVE INSURANCE TO OLD-INSURANCE
MULTIPLY INSURANCE BY .03 GIVING TEMP
ADD INSURANCE,TEMP TO TEMP2
MOVE TEMP2 TO NEW-INSURANCE
MOVE ZEROS TO TEMP, TEMP2
In addition to the COMPUTE suggested by NealB:
MOVE ANNUAL-SALARY TO OLD-SALARY
MULTIPLY ANNUAL-SALARY BY 1.07 GIVING NEW-SALARY
MOVE UNION-DUES TO OLD-DUES
MULTIPLY UNION-DUES BY 1.04 GIVING NEW-DUES
MOVE INSURANCE TO OLD-INSURANCE
MULTIPLY INSURANCE BY 1.03 GIVING NEW-INSURANCE
The use of TEMP2 in the code shown is particularly bad. It firstly relies on its initial value (presumably a VALUE clause) and for the second time through relies upon the 'MOVE ZEROS TO TEMP, TEMP2' at the end of the block. That's a bad way to do it. The repeated initialisation of TEMP is pointless, as TEMP is always the "target" of a GIVING, so its value at the time of the MULTIPLY is irrelevant.

Resources