O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How it works...

Step 1 reads the data, and step 2 plots it as a line chart.

Step 3 uses the diff function to generate single-period differences. It then uses the plot function to plot the differences. By default, the diff function computes single-period differences. You can use the lag argument to compute differences for greater lags. For example, the following calculates two-period lagged differences:

> diff(wmm$Adj.Close, lag = 2) 

Step 4 generates a histogram of one-period price changes. It uses prob=TRUE to generate a histogram based on proportions and then adds on a density plot as well to give a higher-granularity view of the shape of the distribution.

Step 5 computes one-period returns for the stock by dividing the one-period differences ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required