Time series analysis r code how to write out

How to build a time series of stock prices in R

In my case, I would like to do a time series analysis of stock prices in R.

The problem is in the first step, how to convert the stock price into a time series.

The statement I want to use is pri<-ts(data,start=(),frequency=)

But I don’t know how to fill the frequency term?

Because the trading day of the stock is 5 days a week. So how should this frequency be set?

I know that usually frequency=12 for monthly data, frequency=4 for quarterly data, frequency=1 for annual data but I don’t know how to write the daily data

Beginning to learn R language, I hope you guys can help me more.

R language using as.Date want to transform the form of the date as.Date(x,format("%Y%d%m")

In our daily encountered data analysis tasks, we will encounter a lot of data linked to the date and time, such as the month’s daily sales and web page clicks on each time node in a day. Most of this type of data is time series, and time series analysis is common in everyday life. Now let’s talk a little bit about date-time processing in R, and then we’ll learn something about time series analysis if we have time.

One, date function as. The following data is to “* year * month * day” logic input:

>as.Date(“September 28, 2019″, format=”%Y year %m month %d day”)


We see the characters above the %Y and so on, in fact, is the date format of a form of character! The common format is as follows:

The second use is that we are given the starting date, and then enter the number of days delayed, you can output the corresponding date:



Two, time function POSIXct and POSIXlt

(1).POSIXIt main feature: the role is to break up the time, the time is divided into the year, month, day, hour, minute, second, and storage we can combine the unclass () function, so as to extract the date and time information. For example:








$ mday
















We enter the date data with the time, and using the unclass and as.POSIXlt functions we can return the seconds, minutes, hours, days, the number of months that have passed in the year, the number of years that have passed (since 1900 ), the day of the week, the day corresponds to the first day of the year, the time zone and so on.

(2).POSIXct is stored in seconds starting at 8:00 on January 1, 1970, and if it’s negative, it’s the previous datetime; if it’s positive, it’s the one after that, e.g.



attr(, “tzone”)


Three, date and time operations

(1). Subtract dates to get the number of days difference



(2). Subtract dates with time to get the difference (you can specify the units parameter as “secs”, “mins”, “hours”, “days”)

>difftime(‘2019-10-110:00:00’, “2019-10-16:00:00″,units=”hours”)< /p>


How to analyze and forecast a set of seismic data in R language for time series, what is the process of data processing, thanks!

Pixel-by-pixel correlation analysis of long time series raster data in R language

Suppose that there are two sets of raster data, one set represents the monthly rainfall in China in 2019, and one set represents the monthly vegetation leaf area index (LAI) in China in 2019. If you want to get the correlation distribution between monthly rainfall and LAI in China, then you need to perform a raster-by-raster correlation analysis on the raster points corresponding to the two sets of raster data.

The process of importing precipitation data into the raster stack can be understood as stacking the precipitation data in chronological order from top to bottom. Similarly, LAI data is stacked in the same chronological order. It is worth mentioning that the stack() function automatically stacks the raster data according to the order of the file name and the size of the number, and the specific rules can be tried by yourself. Finally, the two raster stacks are merged into one.

Slight changes to the correlation analysis function.

The above method can be generalized, the inputs of linear regression function lm() and correlation analysis function cor() can be vectors, so as long as the function supports vector inputs, theoretically they can be implemented analogously to the above process. But if the function only supports data frame input, such as the gbm package in the function gbm (), then only another way.