Monthly Archives: August 2015

Multiple Lines in One ggplot Graph in R

> library(reshape2)
> library(ggplot2)
> date = data$date
> price1 = data$wti
> price2 = data$ulsd
> df = data.frame(date,price1,price2)
> df2 = melt(data = df,id.vars = "date")
> ggplot(data = df2,aes(x = date,y = value,color = variable)) + geom_line()

ggplot multiple lines

Add ylab and xlab for labels:

> a = ggplot(data = df2,aes(x = date,y = value,color = variable)) + geom_line()
> a + ylab("price")

 

How to Create a Correlation of Multiple Time Series in R

With a matrix of data like this (you should calculate correlations on returns, but absolute prices are used here for the example):

excelpic

Name your desired column vectors:

> wti = data$wti
> brent = data$brent
> lls = data$lls
> mars = data$mars

 

> dataframe = data.frame(wti,brent,lls,mars)
> COR = cor(dataframe)
> head(round(COR,2))
       wti brent  lls mars
wti   1.00  0.97 0.96 0.96
brent 0.97  1.00 0.99 0.99
lls   0.96  0.99 1.00 1.00
mars  0.96  0.99 1.00 1.00

 

If the correlation matrix comes back with “NA”s, use the na.omit command:

> clean = na.omit(dfrm)
> cor(clean)

 

Load “corrplot” package:

> library(corrplot)

 

> corrplot(COR,method = "number")

 

corrplot

See here for more on corrplot:

https://cran.r-project.org/web/packages/corrplot/vignettes/corrplot-intro.html

The R Cookbook has good information.

How to Subset a Vector by Another Vector in R

You have a matrix like this:

excelpic

You want to only look at Q1 prices for the spread between two products.  You can subset the price vectors by the qtr vector like this:

gcq116 = data[data$qtr==1,c("gc87q116")]
rbobq116 = data[data$qtr==1,c("rbobq116")]

 

Create the date vector:

> dateqtr1 = data[data$qtr==1,c("date")]

 

You now have two price vectors showing only the Q1 prices.  Graph the spread between the two vectors like this:

> qplot(dateqtr1,gcq116 - rbobq116)

 

which returns this:

subset prices

Adding Multiple ablines of Different Weights and Colors to Describe Statistics of a Time Series in R

As before, we have a qplot of a time series of prices.  We want to get quantiles of the time series and add ablines at those values.  We have a price vector called “price” and are interested in its 25th, 50th, and 75th percentiles.

> quantile(price,c(.25,.50,.75))
      25%       50%       75% 
-15.73075 -11.94800  -8.73450

 

The 50th percentile is called the median, and we also want to see the mean:

 

> mean(price)
[1] -12.0265

 

The median is greater than the mean, meaning that the price series’ distribution is skewed to the left (more points toward the right but more extreme values to the left).

We can see that by plotting the density function of “price”:

> qplot(price,geom="density")

 

which gives us this graph, which is slightly skewed to the left:

price density

Again, we create the qplot like this:

> m = qplot(date,price)

 

Then we add the ablines at the 25th, 75th, and 50th percentiles, using “lwd” and “col” to customize them:

> m + geom_abline(intercept = -11.95, slope = 0,lwd = 2, col="blue") + geom_abline(intercept = -15.73, slope = 0,lwd=1) + geom_abline(intercept = -8.73,slope = 0,lwd=1)

 

Which returns this graph:

time series abline

Update:

For a more efficient code, embed the statistics in the abline:

> m + geom_abline(intercept = mean(price), slope = 0,lwd = 2, col="blue") + geom_abline(intercept = quantile(price,.25), slope = 0,lwd=1,col="red") + geom_abline(intercept = quantile(price,.75),slope = 0,lwd=1, col="red")

 

Brooks Adams on the Limits of Consolidation and Centralization

Brooks Adams, the great-grandson of John Adams and grandson of John Quincy Adams, saw danger in the consolidation of power in a centralized State, as opposed to the dispersion of power envisioned (by many, but not all, of the founders) at the nation’s founding.  He wtote about it in The Law of Civilization and Decay.  Again from The Conservative Mind:

“Just how far the acceleration of the human movement may go it is impossible to determine; but it seems certain that, sooner or later, consolidation, having reached its limit, will necessarily stop.  There is nothing stationary in the universe.  Not to advance is to go backward, and when a highly centralized society disintegrates under the pressure of economic competition, it is because the energy of the race has been exhausted.”

I might amend Adams’ use of the word “economic” and instead use “political”, for economic power becomes subsumed to political power as that power is centralized.  Economic power is then redistributed from the central power, the State in D.C., back to its original geographic sources in the provinces, but this time to the State’s preferred hands, those who play the game and know how to feed power and what to ask in return.

This centralized State, being short-sighted and greedy, unable to plan, able only to maximize today’s gains at tomorrow’s expense, is doomed to break apart.  It may voluntarily relinquish some of its powers, but that is a sign that it is desperate, and that the relinquishing of power has only begun and will soon take on a life of its own.

 

Facets on a QPlot Time Series

How do you break the previous chart into yearly charts with the same abline?  You use facets.

This is why you should have month, quarter, and year columns in your daily price matrix.

Using the same line of code as before, add the facets command:

> p = qplot(date,wcswti16,facets = .~year)
> p + geom_abline(intercept = -15, slope = 0)

 

Get this:

wcswti16 facets

Time Series with QPlot with a Horizontal Line in R

You want to graph a time series of prices using qplot in the ggplot2 program.

First create the date and price vectors.  The price vector will be the spread between two separate prices:

> date = data$date
> wcswti16 = data$wcs16 - data$wti16

 

Call the qplot, with the date vector as the x-axis and the spread as the y axis, “p”.  Then add an abline to p at the y-axis point of your choice:

> p = qplot(date,wcswti16)
> p + geom_abline(intercept = -15, slope = 0)

 

This is the result:

wcswti16

The Drug War and the Mexican Brain Drain

From Justin Raimondo:

“The drug cartels have inaugurated a reign of terror in Mexico with little resistance from the government. And they are outfitted with military-grade weaponry, including handheld rocket launchers and even makeshift tanks. The government forces, riddled with corruption, have in effect ceded large areas to the cartels, prompting the rise ofcitizens’ militias to fight the criminal gangs alone. The government’s response has been to attack the militias, while leaving the cartels largely alone.

Rife with violence, and increasingly insecure for the Mexican upper and middle classes, Mexico is experiencing a massive brain drain. In the year 2000, there were 300,000 college-educated Mexican nationals living in the US: ten years later there were 530,000. Over a third of those with doctorate degrees live here. In addition to the escalating chaos, Mexican professionals and academics come to the US because their home country doesn’t have the resources, the infrastructure, or the inclination to make it possible for them stay and work in their chosen fields.

The Mexican government is well aware of this problem, and in response has initiated a “Program for Retention and Return.” However, this is unlikely to attract many expatriates unless conditions at home improve markedly and in very short order – or unless Trump is elected President and gets to implement his plan to deport all 11 million “illegals,” strictly limit legal immigration, and dam the southern border.”

The Radical Opponents of Conservatism

Kirk, after giving his six canons of conservative thought, turns to cataloging the schools of radicalism and gives a broad outline of their thoughts.  The schools he gives are:

Their attacks on conservative social order are based on:

  • “The perfectibility of man and the illimitable progress of society:  meliorism
  • “Contempt for tradition.  Reason, impulse, and materialistic determinism are severally preferred as guides to social welfare, trustier than the wisdom of our ancestors”
  • Political levelling.  Order and privilege are condemned; total democracy, as direct as practicable, is the professed radical ideal”
  • Economic levelling.  The ancient rights of property, especially property in land, are suspect to almost all radicals”

Radicals are neoterists.  (The link is to something about post-neoterism, which is more interesting than anything out there about neoterism.)  Change is sought for its own sake.