[ View menu ]

March 2, 2012

How to square numbers in your head

Filed in Encyclopedia ,Ideas ,R
Subscribe to Decision Science News by Email (one email per week, easy unsubscribe)

MENTALLY MULTIPLY NUMBERS BY THEMSELVES

Babbage’s Difference Engine is fueled by squares

Even in the age of ubiquitous computing, its usually faster to do a simple operation like squaring a number in your head as opposed to doing it on paper or firing up R. Everyday decision making in science needs to happen in a fast and frugal manner. Assuming you know your multiplication tables up to 10×10, here’s how to compute the squares of numbers up to 100 in your noggin (and beyond if you are willing to bootstrap).

A. If the number ends in 0, chop off the 0s, square what is left, and put back two 0s for each one you knocked off. Examples:

  • 50: chopping off the zero gives 5, squaring that gives 25, replacing the 0 with two 0s gives 2500
  • 100: chopping off the zero gives 1, squaring that gives 1, replacing each chopped 0 with two gives 10,000

B. If the number ends in 5, chop of the five, take what is left and multiply it by the next highest number, stick 25 on the end Examples:

  • 35: chopping off the 5 gives 3, multiplying what’s left (3) by the next highest number (4) gives 12, sticking 25 on the end gives 1225
  • 95: chopping off the 5 gives 9, multiplying what’s left (9) that by the next highest number (10) gives 90, sticking 25 on the end gives 9025

C. If the number is 1 greater than a number that ends in 5 or 0, first square the number ending in 5 or 0, as above, then add to this the number ending in 5 or 0 and the number. Examples:

  • 51: 51 is one greater than 50, the square of which we know from above is 2500. To this we add the number ending in 5 or 0 (50) and the number (51). 2500 + 50 + 51 = 2601
  • 36: 36 is one greater than 35, the square of which we know from above is 1225. To this we add the number ending in 5 or 0 (35) and the number (36). 1225 + 35 + 36 = 1296

If the number is 1 less than a number that ends in 5 or 0, first square the number ending in 5 or 0, as above, then subtract from this the number ending in 5 or 0 and the number. Examples:

  • 49: 49 is one less than 50, the square of which we know from above is 2500. From this we subtract the number ending in 5 or 0 (50) and the number (49). 2500 – 50 – 49 = 2401
  • 34: 34 is one less than 35, the square of which we know from above is 1225. From this we subtract the number ending in 5 or 0 (35) and the number (34). 1225 – 35 – 34 = 1156

D. If the number is 2 greater than a number that ends in 5 or 0, first square the number ending in 5 or 0, as above, then add to this four times the number that is 1 greater than the number ending in 5 or 0. Examples:

  • 52: 52 is two greater than 50, the square of which we know from above is 2500. To this we add four times the number that is 1 greater than the number ending in 5 or 0 (51). 2500 + 4 * 51 = 2704
  • 37: 37 is two greater than 35, the square of which we know from above is 1225. To this we add four times the number that is 1 greater than the number ending in 5 or 0 (36). 1225 + 4 * 36 = 1369

If the number is 2 less than a number that ends in 5 or 0, first square the number ending in 5 or 0, as above, then subtract from this four times the number that is 1 less than the number ending in 5 or 0. Examples:

  • 48: 48 is two less than 50, the square of which we know from above is 2500. From this we subtract four times the number that is 1 less than the number ending in 5 or 0 (49). 2500 – 4 * 49 = 2304
  • 33: 33 is two less than 35, the square of which we know from above is 1225. From this we subtract four times the number that is 1 less than the number ending in 5 or 0 (34). 1225 – 4 * 34 = 1089

Since every number is either 1 or 2 greater or less than a number ending in 0 or 5, we are done.

Happy squaring!

Proof of C: First part: (N+1)^2=N^2+N+(N+1), Second part (N-1)^2=N^2-N-N+1=N^2-N-(N-1)
Proof of D: First part: (N+2)^2=N^2+4N+4=N^2+4(N+1), Second part (N-2)^2=N^2-4N+4=N^2-4(N-1)

Photo credit: http://www.flickr.com/photos/mrgiles/325903759/

February 22, 2012

Rules of thumb for losing weight

Filed in Ideas
Subscribe to Decision Science News by Email (one email per week, easy unsubscribe)

SIMPLE RULES FOR DIETING

This is what we’ve figured out from our experiments.

  • Live 1.5 miles from your workplace. It’s too short to drive, so you just walk.
  • Stop deciding what to eat for a week. Worked for us.
  • Eat the same thing for breakfast every day (source: Brian Wansink). He’s found correlational evidence for this.
  • Use small plates (source: Brian Wansink). Experiments show that this affects consumption (at least during the experiment).
  • Put money on the line (source: Daniel Reeves). With enough money on the line, it seems like it would just have to work. Beeminder.com is our nerdy favorite. Stickk.com is also good
  • Weigh your food with a gram scale. Pouring 20 grams less cereal in the AM makes a difference. Also, a gram scale is the key to making the perfect pot of coffee. We have this one, among others.
  • Learn to estimate calories. We’re making a video game of this, but fitday.com is a fun place to start.
  • Don’t eat more than 500 calories at a time. You’re probably not hungry 15 minutes after stopping.
  • Round the calories in fruits and vegetables down to zero. WeightWatchers has apparently moved to this in their new point system. It’s obviously wrong, but it works.

photo credit:http://www.flickr.com/photos/puuikibeach/4470122675

February 13, 2012

Max Planck Summer Institute on Bounded Rationality, July 3-10, 2012, Berlin

Filed in Conferences ,Ideas ,Programs
Subscribe to Decision Science News by Email (one email per week, easy unsubscribe)

MPI SUMMER SCHOOL 2012

Summer Institute on Bounded Rationality

Foundations for an Interdisciplinary Decision Theory

3 – 10 July, 2012

Directed by Gerd Gigerenzer

Center for Adaptive Behavior and Cognition

Max Planck Institute for Human Development, Berlin, Germany

It is our pleasure to announce the upcoming Summer Institute on Bounded Rationality 2012 – Foundations of an Interdisciplinary Decision Theory, which will take place from July 3 – 10, 2012 at the Max Planck Institute for Human Development in Berlin.

The Summer Institute will provide a platform for genuinely interdisciplinary research, bringing together young scholars from psychology, biology, philosophy, economics, and other social sciences. Its focus will be on “decision making in the wild” – how cognition adapts to real-world decision-making environments. One of its aims is to provide participants a deeper understanding of the way humans come to grips with a fundamentally uncertain world, with an emphasis on applied contexts such as social interactions, medicine, justice, business, and politics.

Talented graduate students and postdoctoral fellows from around the world are invited to apply by March 31, 2012. We will provide all participants with accommodation and stipends to cover part of their travel expenses. Details on the Summer Institute and the application process are available at http://www.mpib-berlin.mpg.de/en/research/adaptive-behavior-and-cognition/summer-institute-2012

Please pass on this information to potential candidates from your own department or institute.

February 5, 2012

The art and value of the essay

Filed in Ideas
Subscribe to Decision Science News by Email (one email per week, easy unsubscribe)

NEWS IS A COMMODITY

We just realized something. One of those “no duh” moments.

If you are on a News site and there is a link to an article like “Tens of Thousands Protest in Moscow” and it has a little padlock symbol next to it meaning that it is for subscribers only, it is not a big deal. You can just type “thousands protest moscow” into a search engine and find out what is going on.

If, however, the story under lock and key is called something provocative like “Why Chinese Mothers are Superior” or “Why French Parents are Superior”, well, then you would be screwed. (*)

If you are going to make money selling access to content on the web, you need to offer things that can’t be found elsewhere.

To all those who excel in the art of the essay, this is your era.

(*) Yes, those article are free to view online, but suppose they weren’t. I admit that maybe I find the titles more interesting than you do, but I’m thinking about my three year old all day.

Photo credit:http://www.flickr.com/photos/kristiand/3223920178/

February 4, 2012

Are you Risk Literate?

Filed in Research News ,Tools
Subscribe to Decision Science News by Email (one email per week, easy unsubscribe)

THE BERLIN NUMERACY TEST

We’ve written before about the challenges of communicating risks. Can people understand the risks inherent in their savings plans, loans, surgeries, or medications? This week, researchers have published a new instrument designed to very quickly assess exactly that (www.riskliteracy.org).

We introduce the Berlin Numeracy Test, a new psychometrically sound instrument that quickly assesses statistical numeracy and risk literacy. We present 21 studies (n=5336) showing robust psychometric discriminability across 15 countries (e.g., Germany, Pakistan, Japan, USA) and diverse samples (e.g., medical professionals, general populations, Mechanical Turk web panels).

The authors report that the test is the strongest predictor of comprehension of many everyday risks (e.g., evaluating claims about products and treatments; interpreting forecasts) doubling the predictive power of dozens of other commonly used tools (e.g., intelligence, personality, motivation, decision styles). They argue that by quickly assessing risk literacy and statistical numeracy, researchers can adaptively deliver more accessible information about risks in health, finance, and technology.

For example, after a couple questions people might get interactive brochures that are custom-tailored for their skill set. Alternatively, doctors, financial advisors, and lawyers might use similar fast tests to get a sense of the appropriate level of discourse for their diverse clients. The test can also be used to simply learn more about one’s own risk literacy.

To take the test go to www.riskliteracy.org and in about 2-3 minutes you’ll get feedback about your levels of risk literacy as compared to educated people and professionals from around the world.

January 28, 2012

Exercise equivalents over calorie counts

Filed in Ideas ,Research News
Subscribe to Decision Science News by Email (one email per week, easy unsubscribe)

250 CALORIES VS 30 MINUTES OF JOGGING

New York has, in the last years, joined the list of cities that require calorie counts to be posted on the menus of chain restaurants. Early research suggests that the labeling is not terribly effective, though Decision Science News is skeptical of the skepticism and thinks that long-run effects will emerge.

In any case, if posted calorie counts were really the sledgehammer manipulation they were hoped to be, they’d show an immediate effect. So what are behavioral economists to do? Try related policies.

Sara Bleich, an assistant professor of health policy at the Johns Hopkins Bloomberg School of Public Health, tried an innovative intervention and got some promising results. Her team compare calorie counts to exercise equivalents, so they tried signs saying that a soda:

  • Contained 250 calories
  • Contained 11 percent of daily calories
  • Would take 50 minutes of exercise to burn off*

They found that the exercise message reduced soda purchases by 40 percent while the other framings had no effect.

* (According to fitday.com, which Decision Science News trusts and uses, 30 minutes jogging burns 250 calories, so perhaps the study’s results were obtained by exaggerating the time needed)

REFERENCES

Photo credit: http://www.flickr.com/photos/lipsss/3196111994/

January 17, 2012

Some code to help you remember numbers

Filed in Encyclopedia ,Ideas ,Programs ,R
Subscribe to Decision Science News by Email (one email per week, easy unsubscribe)

HANDY COMPUTER PROGRAMS FOR TURNING NUMBERS INTO MEMORABLE WORDS

Two posts ago we showed you the digit sound system for remembering numbers. This week we provide two Python programs (and one R program) to help you create mnemonics.

w2num.py – converts every word in the English language to a number
find.py – takes a longish number and gives you words that when put together translate into that number using the digit-sound system.

If you are not a programmer, or if you are lazy, you can get to the chase and just download the relevant outupt files here

wordNum.txt – every word in English translated into a number using the digit-sound system
numWords.txt – All the English words that translate into a given number

However, you have a long number that you want to make a mnemonic for, you’re better off using find.py or find.R below to find mnemonics. These handy programs break up long numbers into shorter ones which do have mnemonics.

NOTES:
These programs use phonetics (not spelling) to make the mnemonics. Read the small print on Jap Murre’s site to see why “letter” is 514 and not 5114 and why “ing” is 27. Thanks to Daniel Reeves for convincing discussions on the merits of using a purely phonetic system.

As usual, we’re open for suggestions on how to improve these programs … they’re both kind of slapdash jobs.

##w2num.py##
Prerequisites: You need to save a couple of wordlists locally. In particular
1) You need to save a copy of http://bit.ly/xz8YIC
as cmudict-0.4.scm. This is a mapping from words in English to phonemes.
2) You need to save a copy of http://scrapmaker.com/data/wordlists/twelve-dicts/2of12inf.txt
as wordlist.txt. This is a list of reasonably common English words (b/c some of the words in cmudict are too rare to be useful).
3) Put cmudict-0.4.scm and wordlist.txt in the same directory as
w2num.py and then do
python w2num.py > wordNum.txt
to generate the number corresponding to every English word. (If in the future these URLs break, just search around for copies of cmudict-0.4.scm and 2of12inf.txt …there are many copies of these dictionaries around.)

##find.py##
This is the program you use when you have a longish number and you want to find words that encode it. It uses recursive ™ technology to break up numbers a few different ways to increase your chances of finding a mnemonic you like.
Prerequisite: You must have created wordNum.txt (by running python w2num.py > wordNum.txt) in the previous step and saved it to the same directory as this program before running.
To turn a number like 8675309 into words do
python find.py 8675309

Lastly, for kicks I wrote a slightly dumber version of find.py in R. It lacks the cool “rec3” method of finding mnemonics but it gets the job done.

##find.R##
Same as find.py
Prerequisite: same as find.py
To turn a number like 8675309 into words change the variable mstr to ‘8675309’ and run the program.

January 12, 2012

SJDM newsletter ready to download

Filed in Conferences ,Gossip ,Ideas ,Jobs ,SJDM
Subscribe to Decision Science News by Email (one email per week, easy unsubscribe)

SOCIETY FOR JUDGMENT AND DECISION MAKING NEWSLETTER

 

Just a reminder that the Quarterly Society for Judgment and Decision Making newsletter can be downloaded from the SJDM site:

http://sjdm.org/newsletters/

Enjoy!

January 6, 2012

How to remember numbers

Filed in Encyclopedia ,Ideas ,Research News
Subscribe to Decision Science News by Email (one email per week, easy unsubscribe)

RESOLVE TO MEMORIZE NUMBERS WITH THE DIGIT-SOUND METHOD

At Decision Science News, we have all kinds of numbers memorized: IP addresses, passport numbers, phone numbers, bank account numbers, logarithms, etc. Once you have stuff like this memorized, you’ll  realize how much of a hassle it was to look all this stuff up all the time. Besides, it’s fun.

But how is it done?

It is done with the digit-sound method, which we learned from Professor Jaap Murre’s neuroMod site at the University of Amsterdam. According to Murre, this amazing method traces back to 1648!

You associate a digit with a sound or sounds like this

0 z, s, soft-c ‘z’ is the first letter of zero
1 t, th, d ‘t’ has one downstroke
2 n ‘n’ has two legs (downstrokes)
3 m ‘m’ has three legs (downstrokes)
4 r ‘r’ is the last sound in fouR
5 l ‘l’ or L is the Roman numeral 50
6 soft-g, j, sh, ch ‘g’ when turned around gives ‘6’
7 k, q, hard-c, hard-g ‘k’ is made of two 7’s back-to-back
8 f, v ‘f’ when written in script looks like ‘8’
9 p, b ‘p’ when turned around gives ‘9’

Then, when you want to remember a number, you simply replace the digits with sounds and try to make a vivid phrase. Any vowels or sounds not included in the chart are ignored when you are reading back the phrase. For example, suppose you want to memorize Jenny’s phone number: 867-5309.

Here’s how we might do it:

  • Change 867 to sounds, such as “f”, “sh” and “k”
  • Change 5309 to sounds, such as “l”, “m”, “s” and “b”
  • Now insert vowels and letters not on the chart in order to generate a vivid phrase from those sounds.

We came up with the following dialog involving a parent and child at the dinner table:

–“Fish, ok?”
–“Lame! *sob*”

In this mini play, we have “f”, “sh”, “k” “l”, “m”, “s” and “b” in order, and can easily reconstruct 867-5309. Remember, when reading it back, you ignore all vowels and letters that aren’t in the chart.

The only start-up cost is memorizing the chart, but that’s not hard. There’s even a training tool so you can practice until you have the associations down pat.

Of course, since there can be a few possible sounds for each letter, you need to play around a bit to find a phrase that works for you. To generate possibilities for long numbers, we’ve written the following python script.

NOTE 1: In this code, we’ve simplified the digit-sound method to include fewer letters since we always get confused with hard vs soft letters. We find the simplified method works just fine for us.
NOTE 2: This code is written in a hackish, randomized way that generates wasteful dupes. However, we don’t care since it doesn’t output the dupes, it runs super fast, and if you have a huge number of possibilities, you don’t want to see them all anyway. That said, if people have better implementations, we’re open for suggestions. Thanks to Dan Reeves for tightening up the code a bit!
NOTE 3: You can take the output of this program and hunt for words that fit the patterns with a linux call like this.
grep -E ‘^l[aieouy]*m[aieouy]*n[aeiouy]*$’ wordlist.txt
Where you are looking for words that match “l”, “m”, “n” and wordlist.txt is a list of words, one per line. You can find many wordlists here.

#Usage. Save this file as generate.py
#Then to use, do generate.py 867 (if 867 is the number for
#which you want to generate letters)
from cStringIO import StringIO
import random
from sys import argv
from time import strftime
mstr=str(argv[1])
def genString():
        numsounds = [['s','z'], ['t','th','d'],['n'],['m'],['r'],\
['l'],['j','ch','sh',],['k','q'],['f','v'],['p','b']]
        return [random.choice(numsounds[int(i)]) for i in mstr]
i=0
myDict={}
while i<10000:
        astr=' '.join(genString())
        if not myDict.has_key(astr):
                myDict[astr]=1
                print '%-20s ==> %2d : %20s' % \
(astr,i,strftime("%Y-%m-%d %H:%M:%S"))
        i=i+1

Photo credit: http://memory.uva.nl/memimprovement/eng/phon_peg_1.htm

December 29, 2011

You’ve got the whole world in your portfolio

Filed in Encyclopedia ,R ,Tools
Subscribe to Decision Science News by Email (one email per week, easy unsubscribe)

LEARN HOW TO IMPORT WORLD BANK DATA AND INVEST IN THE WHOLE WORLD


Click to enlarge
The market cap of the countries comprising 90% of the world’s market cap (end 2010)

A famous finance professor once told us that good diversification meant holding everything in the world. Fine, but in what proportion?

Suppose you could invest in every country in the world. How much would you invest in each? In a market-capitalization weighted index, you’d invest in each country in proportion to the market value of its investments (its “market capitalization”). As seen above, the market-capitalization of the USA is about 30%, which would suggest investing 30% of one’s portfolio in the USA. Similarly, one would put 8% in China, and so on. All this data was pulled from the World Bank, and at the end of this post we’ll show you how we did it.

What makes life easy is that economists have grouped countries into regions and the market caps of each can be computed by summing the market caps of the constituent countries. See this MSCI page for the manner in which we’ve categorized countries in this post:

North America: USA, Canada
Emerging: Brazil, Chile, China, Colombia, Czech Republic, Egypt, Hungary, India, Indonesia, Korea, Malaysia, Mexico, Morocco, Peru, Philippines, Poland, Russia, South Africa, Taiwan, Thailand, Turkey
Asia: Japan, Australia, Hong Kong, New Zealand, Singapore
Europe: Austria, Belgium, Denmark, Finland, France, Germany, Greece, Ireland, Italy, Netherlands, Norway, Portugal, Spain, Sweden, Switzerland, United Kingdom
Other: Everybody else in the world

What makes life easier still is that one can purchase ETFs that work like indices tracking the various regions of the world. For example at Vangard one can purchase

  • Vanguard Total Stock Market ETF (Ticker VTI), which basically covers North America
  • Vanguard MSCI Emerging Marketing ETF (Ticker VWO), which covers emerging markets
  • Vanguard MSCI Pacific ETF (Ticker VPL), which covers Asia (minus the Asian emerging markets countries)
  • Vanguard MSCI Europe ETF (Ticker VGK), which covers Europe

What makes life a bit more complicated is that not everybody agrees on market-cap weighting. Some favor Gross Domestic Product (GDP) weighting for the reasons given at this MSCI page.

  • GDP figures tend to be more stable over time compared to equity markets’ performance-related peaks and troughs
  • GDP weighted asset allocation tends to have higher exposure to countries with above average economic growth, such as emerging markets
  • GDP weighted indices may underweight countries with relatively high valuation, compared to market-cap weight indices

Here are the countries that make up 90% of the world’s GDP:


Click to enlarge
The GDP of the countries comprising 90% of the world’s GDP (end 2010)

Because we love you, we’ve also computed how the classic market regions compare to one another in terms of GDP

If you track changes in market cap from month to month as we do, you’ll appreciate the stability that GDP weighting provides. For example, the market cap data in this post, from 2010, is already way out of date.

So, weight by market cap, or weight by GDP, or do what we do and weight by the average of market cap and GDP, but don’t wait to diversify your portfolio outside of a handful of investments you happen to own. Invest in the world.

For those who love R and who want to learn how to pull down World Bank data, the code is provided here. I got started with this by borrowing code from this very helpful blog post by Markus Gesmann and tip my hat to its author. Thanks also to Hadley for writing ggplot2 (and for visiting us at Yahoo the other week. And thanks to Yihui Xie for his code formatter.

R CODE

library(ggplot2)
library(car)
library(RJSONIO)
ftse=read.delim("201112_FTSE_Market_Caps.tsv",sep="\t")
##See www.msci.com/products/indices/tools/index.html
##Emerging::Brazil, Chile, China, Colombia, Czech Republic, Egypt, Hungary,
#India, Indonesia, Korea, Malaysia, Mexico, Morocco, Peru, Philippines,
#Poland, Russia, South Africa, Taiwan, Thailand, Turkey
##Europe::Austria, Belgium, Denmark, Finland, France, Germany, Greece, Ireland, Italy,
#Netherlands, Norway, Portugal, Spain, Sweden, Switzerland, United Kingdom
##Asia::Japan, Australia, Hong Kong, New Zealand, Singapore
recodestring=" c('USA','CAN')='North America';
c('BRA','CHL','CHN','COL','CZE','EGY','HUN','IND','IDN','KOR','MYS','MEX','MAR','PER','PHL','POL',
'RUS','ZAF','TWN','THA','TUR')= 'Emerging';
c('AUT','BEL','DNK','FIN','FRA','DEU','GRC','IRL','ITA',
'NLD','NOR','PRT','ESP','SWE','CHE','GBR')= 'Europe';
c('JPN','AUS','HKG','NZL','SGP')= 'Asia';
else='Other'"
#How much of the world do we want to consider (to keep graphs manageable)
CUTOFF=.9
#Some code from internet to read world bank data. Adapted from
#http://www.r-bloggers.com/accessing-and-plotting-world-bank-data-with-r/
#See also http://data.worldbank.org/indicator/NY.GDP.MKTP.CD
getWorldBankData = function(id='NY.GDP.MKTP.CD', date='2010:2011',
value="value", per.page=12000){
require(RJSONIO)
url = paste("http://api.worldbank.org/countries/all/indicators/", id,
"?date=", date, "&format=json&per_page=", per.page,
sep="")
wbData = fromJSON(url)[[2]]
wbData = data.frame(
year = as.numeric(sapply(wbData, "[[", "date")),
value = as.numeric(sapply(wbData, function(x)
ifelse(is.null(x[["value"]]),NA, x[["value"]]))),
country.name = sapply(wbData, function(x) x[["country"]]['value']),
country.id = sapply(wbData, function(x) x[["country"]]['id'])
)
names(wbData)[2] = value
return(wbData)
}
getWorldBankCountries = function(){
require(RJSONIO)
wbCountries =
fromJSON("http://api.worldbank.org/countries?per_page=12000&format=json")
wbCountries = data.frame(t(sapply(wbCountries[[2]], unlist)))
levels(wbCountries$region.value) = gsub("\\(all income levels\\)",
"", levels(wbCountries$region.value))
return(wbCountries)
}
### Get the most recent year with data
curryear = as.numeric(format(Sys.Date(), "%Y"))
years = paste("2010:", curryear, sep="")
##Get GDP for the countries (GDP in Current US Dollars)
GDP = getWorldBankData(id='NY.GDP.MKTP.CD',
date=years,
value="gdp")
#If a year has > 60 NAs it is not ready
while( sum(is.na(subset(GDP,year==curryear)$gdp)) > 60 )
{curryear=curryear-1}
#Keep just the most current year
GDP=subset(GDP,year==curryear);
## Get country mappings
wbCountries = getWorldBankCountries()
## Add regional information
GDP = merge(GDP, wbCountries[c("iso2Code", "region.value", "id")],
by.x="country.id", by.y="iso2Code")
## Filter out the aggregates, NAs, and irrelevant columns
GDP = subset(GDP, !region.value %in% "Aggregates" & !is.na(gdp))
GDP=GDP[,c('year','gdp','country.name','region.value','id')]
GDP=GDP[order(-1*GDP[,2]),]
GDP$prop=with(GDP,gdp/sum(gdp))
GDP$cumsum=cumsum(GDP$gdp)
GDP$cumprop=with(GDP,cumsum(gdp)/sum(gdp))
GDP$market.region=recode(GDP$id,recodestring)
resize.win <- function(Width=6, Height=6) { plot.new() dev.off(); #dev.new(width=6, height=6) windows(record=TRUE, width=Width, height=Height) } slice=max(which(GDP$cumprop<=CUTOFF)) smGDP=GDP[1:slice,] smGDP$country.name=factor(smGDP$country.name,levels=unique(smGDP$country.name)) smGDP$region.value=factor(smGDP$region.value,levels=unique(smGDP$region.value)) smGDP$market.region=factor(smGDP$market.region,levels=unique(smGDP$market.region)) g=ggplot(smGDP,aes(x=as.factor(country.name),y=prop)) g=g+geom_bar(aes(fill=market.region)) g=g+opts(axis.text.x=theme_text(angle=90, hjust=1))+xlab(NULL)+ ylab( paste("Proportion of global GDP\nTop ",CUTOFF*100,"% countries",sep="")) g=g+opts(legend.position=c(.8,.8)) resize.win(5,5) #might have to run this twice to get it to shift from the default window g ggsave("GDPbyCountry.png",g,dpi=600) smGDPsum=ddply(GDP,.(market.region),function(x) {sum(x$gdp)}) smGDPsum$V1=smGDPsum$V1/sum(smGDPsum$V1) smGDPsum=smGDPsum[order(-1*smGDPsum[,2]),] smGDPsum$market.region=factor(smGDPsum$market.region,levels=c("North America","Emerging","Asia","Europe","Other")) g=ggplot(smGDPsum,aes(x=as.factor(market.region),y=V1)) g=g+geom_bar(aes(fill=market.region)) g=g+opts(axis.text.x=theme_text(angle=90, hjust=1))+xlab(NULL)+ylab("GDP of Countries by Market Region\n") g=g+opts(legend.position="none") resize.win(5,5) #might have to run this twice to get it to shift from the default window g ggsave("GDPbyRegion.png",g,dpi=600) ##Get Market Cap data from World Bank CM.MKT.LCAP.CD ### Get the most recent year with data curryear = as.numeric(format(Sys.Date(), "%Y")) years = paste("2010:", curryear, sep="") ##Get MarketCap for the countries (MCP in Current US Dollars) MCP = getWorldBankData(id='CM.MKT.LCAP.CD', date=years, value="mcp") #If a year has > 60 NAs it is not ready
while( sum(is.na(subset(MCP,year==curryear)$mcp)) > 150 )
{curryear=curryear-1}
#Keep just the most current year
MCP=subset(MCP,year==curryear);
## Add regional information
MCP=merge(MCP, wbCountries[c("iso2Code", "region.value", "id")],
by.x="country.id", by.y="iso2Code")
## Filter out the aggregates, NAs, and irrelevant columns
MCP = subset(MCP, !region.value %in% "Aggregates" & !is.na(mcp))
MCP=MCP[,c('year','mcp','country.name','region.value','id')]
MCP=MCP[order(-1*MCP[,2]),]
row.names(MCP)=NULL
MCP$prop=with(MCP,mcp/sum(mcp))
MCP$cumsum=cumsum(MCP$mcp)
MCP$cumprop=with(MCP,cumsum(mcp)/sum(mcp))
MCP$market.region=recode(MCP$id, recodestring)
slice=max(which(MCP$cumprop<=CUTOFF)) smMCP=MCP[1:slice,] smMCP$country.name=factor(smMCP$country.name,levels=unique(smMCP$country.name)) smMCP$region.value=factor(smMCP$region.value,levels=unique(smMCP$region.value)) smMCP$market.region=factor(smMCP$market.region,levels=unique(smMCP$market.region)) g=ggplot(smMCP,aes(x=as.factor(country.name),y=prop)) g=g+geom_bar(aes(fill=market.region)) g=g+opts(axis.text.x=theme_text(angle=90, hjust=1))+xlab(NULL)+ ylab( paste("Proportion of global Market Cap\nTop ",CUTOFF*100,"% countries",sep="")) g=g+opts(legend.position=c(.8,.8)) resize.win(5,5) #might have to run this twice to get it to shift from the default window g ggsave("MCPbyCountry.png",g,dpi=600) smMCPsum=ddply(MCP,.(market.region),function(x) {sum(x$mcp)}) smMCPsum$V1=smMCPsum$V1/sum(smMCPsum$V1) smMCPsum=smMCPsum[order(-1*smMCPsum[,2]),] smMCPsum$market.region=factor(smMCPsum$market.region,levels=c("North America","Emerging","Asia","Europe","Other")) g=ggplot(smMCPsum,aes(x=as.factor(market.region),y=V1)) g=g+geom_bar(aes(fill=market.region)) g=g+opts(axis.text.x=theme_text(angle=90, hjust=1))+xlab(NULL)+ylab("Market Cap by Market Region\n") g=g+opts(legend.position="none") resize.win(5,5) #might have to run this twice to get it to shift from the default window g ggsave("MCPbyRegion.png",g,dpi=600)