Fitting a harmonic function into a time series in R [closed] - r

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I got the following time series of residuals from another regression.
One index is a day. You can directly observe the year cycle.
Aim is to fit a harmonic function through it to expalain further part of the underlying time series.
I really appreciate your ideas about which function to use for estimating the right parameters! From acf we learn that there is also a week cycle. However, this issue i will adress later with sarima.

This seems to be the sort of thing a fourier transform is designed for.
Try
fftobj = fft(x)
plot(Mod(fftobj)[1:floor(length(x)/2)])
The peaks in this plot corresponds to frequencies with high coefficients in the fit. Arg(fftobj) will give you the phases.

Well i tried it, but it provides a forecast that looks like a exponential distribution. I solved the problem meanwhile in another way. I added a factor component for each month and draw a regression. In the next step I smoothed the results from this regression and got a intra-year pattern that is more accurate than a harmonic function. E.g. during the June and July (around 185) there is generally a low level but also a high amount of peaks.

Related

Package for Converting Time Series to be Stationary in R [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
Are there any packages in R out there that will do the work of transforming a uni-variate or bi-variate time series to be stationary?
Thanks; any help would be greatly appreciated!
Is there a one for all package with a bunch of different functions to convert non stationary time series to stationary? No (As far as I know)
Its all about the data and figuring out what method would work.
To check if your time series is stationary - can try box.test, adf.test or kpss.test
Did you try diff()? diff calculates the differences between all consecutive values of a vector.
"One way to make a non-stationary time series stationary — compute the differences between consecutive observations. This is known as differencing." - from link
Another way would be log() transformation which is often used with diff().
Other methods are square, log difference, lag. Could try different combinations of those techniques for example log square difference or try other things like Box-Cox transformations.

Right Way to Extrapolate in R [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
I have a time-series data containing 16 values (of no. of Vehicles) from 2001 to 2016. I wanted to predict - based on the underlying trend - the values upto 2050 (which is a long shot I agree).
Upon doing some research, I found that it can be done by methods like HoltWinters or TBATS which, even though, did not go with my own plan of using some Machine Learning algorithm.
I am using R for all my work. Now, after using HoltWinters() and then forecast() methods, I did get an extrapolated curve uptil 2050 but it is a simple exponential curve from 2017 to 2050 which I think I could have obtained through meager calculations.
My question is twofold:
1) What would be the best approach to obtain a meaningful extrapolation?
2) Does my current approach be modified to give me a more meaningful extrapolation?
By meaningful I want to express that a curve with the details more closer to actuality.
Thanks a lot.
I guess you need more data to make predictions. HoltWinters or TBATS may work but there are many other ML models for time series data you can try.
http://a-little-book-of-r-for-time-series.readthedocs.io/en/latest/src/timeseries.html
This link has the R sample code for Holtwinters and the plots.

How to identify the distribution of the given data using r [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 10 months ago.
The community reviewed whether to reopen this question 10 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I have the data as below and i need to identify the distribution of the data. pls help.
x <- c(37.50,46.79,48.30,46.04,43.40,39.25,38.49,49.51,40.38,36.98,40.00,38.49,37.74,47.92,44.53,44.91,44.91,40.00,41.51,47.92,36.98,43.40)
A neat approach would involve using fitdistrplus package that provides tools for distribution fitting. On example of your data.
library(fitdistrplus)
descdist(x, discrete = FALSE)
Now you can attempt to fit different distributions. For example:
normal_dist <- fitdist(x, "norm")
abs subsequently inspect the fit:
plot(normal_dist)
As a generic point I would suggest that you have a look at this discussion at Cross Validated, where the subject is discussed at lengths. You may be also willing to have a look at a paper by Delignette-Muller and Dutang - fitdistrplus: An R Package for Fitting Distributions, available here if you are interested in a more detailed explanation on how to use the Cullen and Frey graph.
First, thing you can do is to plot the histogram and overlay the density
hist(x, freq = FALSE)
lines(density(x))
Then, you see that the distribution is bi-modal and it could be mixture of two distribution or any other.
Once you identified a candidate distribution a 'qqplot' can help you to visually compare the quantiles.

Time series forecasting of price of financial instrument [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I am working with a financial instrument's intraday time series data. I have to predict the price of a financial instrument on the basis of some statistical parameters(Var1, Var2, Var3) and of time series intraday(Obeserv.1, Observ.2.......Observ.80) data of the previous period. I have to predict the price of financial instrument in 81st period.
All lines in the table are mixed so that the information in any i-line is useless for prediction of the j-line.
I am planning to solve this problem by using R. I am new into this financial modelling field. What approach I can take for prediction. Please help me out for this.
Data set looks like that
Sample Data
In the past I've done this kind of thing for a living. The general concept is to think of a model that would have some predictive power, then fit e.g. the first half of the data set to the model, and finally test to see whether the model has any predictive power on the second half of the data set.
If you haven't tried anything before, a good place to start is with ARMA models, (see e.g. AutoRegressive–Moving-Average model on wikipedia).

Should Categorical predictors within a linear model be normally distributed? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I am running simple linear models(Y~X) in R where my predictor is a categorical variable (0-10). However, this variable is not normally distributed and none of the transformation techniques available are healpful (e.g. log, sq etc.) as the data is not negatively/positively skewed but rather all over the place. I am aware that for lm the outcome variable (Y) has to be normally distributed but is this also required for predictors? If yes, any suggestions of how to do this would be more than welcome.
Also, as the data I am looking at has two groups, patients vs controls (I am interested in group differences, as you can guess), do I have to look at whether the data is normally distributed within the two groups or overall across the two groups?
Thanks.
See #Roman Luštriks comment above: it does not matter how your predictors are distributed. (Except for problems with multicollinearity.) What is important is that the residuals be normal (and with homogeneous variances).

Resources