(This article was first published on ** MilanoR**, and kindly contributed to R-bloggers)

MilanoR staff is happy to announce the next MilanoR meeting.

**Thursday, December 18, 2014**

from 6 to 8 pm

**Welcome Presentation**

by Nicola Sturaro

Consultant at Quantide

**Shine your Rdata: multi-source approach in media analysis for telco industry**

by Giorgio Suighi (Head Of Analytics), Carlo Bonini (Data Scientist) and Paolo Della Torre (ROI Manager), MEC

The second speaker will be announced soon. If you follow R blogs or tweets may be you already know his/her name. Otherwise, you should wait until Monday. Stay connected!

**Fiori Oscuri Bistrot & Bar**

Via Fiori Oscuri, 3 - Milano (Zona Brera)

Our sponsors will provide the buffet after the meeting.

MilanoR meeting is sponsored by

MilanoR is a free event, open to all R users and enthusiasts or those who wish to learn more about R. Places are limited so, if you would like to attend to the MilanoR meeting, please register below. (If you're reading this post from a news feed, e.g. from R-bloggers, please visit the original post in the MilanoR website to see the form and subscribe the event)

[contact-form-7]

To **leave a comment** for the author, please follow the link and comment on his blog: ** MilanoR**.

R-bloggers.com offers

(This article was first published on ** Revolutions**, and kindly contributed to R-bloggers)

If you've lived in or simply love London, a wonderful new book for your coffee-table is *London: The Information Capital*. In 100 beautifully-rendered charts, the book explores the data that underlies the city and its residents. To create most of these charts, geographer James Cheshire and designer Oliver Uberti relied on programs written in R. Using the R programming language not only created beautiful results, it saved time: "a couple of lines of code in R saved a day of manually drawing lines".

Take for example *From Home To Work, *the graphic illustrating the typical London-area commute. R's ggplot2 package was used to draw the invidual segments as transparent lines, which when overlaid build up the overall picture of commuter flows around cities and towns. The R graphic was then imported into Adobe Illustrator to set the color palette and add annotations. (FlowingData's Nathan Yau uses a similar process.)

Another example is the chart below of cycle routes in London. (We reported on an earlier version of this chart back in 2012.) As the authors note, "hundreds of thousands of line segments are plotted here, making the graphic an excellent illustration of R’s power to plot large volumes of data."

You can learn more from the authors about how R was used to create the graphics in *London: The Information Capital* and see several more examples at the link below. And if you'd like a copy, you can buy the book here.

London: The Information Capital / Our Process: The Coder and Designer

To **leave a comment** for the author, please follow the link and comment on his blog: ** Revolutions**.

R-bloggers.com offers

(This article was first published on ** lukemiller.org » R-project**, and kindly contributed to R-bloggers)

I’ve written previously about some example R scripts I created to extract sea surface temperature data from NOAA’s Optimum Interpolated Sea Surface Temperature products. If you want daily global sea surface temperatures on a 0.25×0.25° grid, they gather those into 1-year files available at http://www.esrl.noaa.gov/psd/data/gridded/data.noaa.oisst.v2.highres.html. If you want weekly average SST values on a 1×1° […]
To **leave a comment** for the author, please follow the link and comment on his blog: ** lukemiller.org » R-project**.

R-bloggers.com offers

(This article was first published on ** Stat Tech » R**, and kindly contributed to R-bloggers)

The gridGraphics package provides a function, grid.echo(), that can be used to convert a plot drawn with the graphics package to the same result drawn using grid. This provides access to a variety of grid tools for making customisations and additions to the plot that are not possible with the graphics package.
Paul Murrell
Download.
To **leave a comment** for the author, please follow the link and comment on his blog: ** Stat Tech » R**.

R-bloggers.com offers

(This article was first published on ** TC » R**, and kindly contributed to R-bloggers)

RStudio has a number of keyboard shortcuts that make for cleaner and faster coding. I put all the Windows shortcuts that I use onto a single page so that I can pin them next to my computer.

Some favourites of mine are:

**Using code sections/chunks -** Use Ctrl+Shift+R to insert a code section and a popup box will appear for you to name that section. Ctrl+Alt+T runs the current code section. When you are done working on a code section you can ‘fold’ it up to improve the readability of your file (Alt+L is fold current code section, Alt+O is fold all sections).

**Re-running code quickly – **Ctrl + Shift + P will execute the same region of code that was just previously run with the changes made since then.

**Deleting/moving stuff faster – **Ctrl+D deletes an entire line. Ctrl + backspace deletes the current word as in most word processing software. Alt + up/down moves code up and down lines in the console while Shift+Alt+up/down copies lines up/down.

**Switch between plots – **To toggle between plots use Ctrl+Shift+PgUp/PgDn (It’s a lot faster than using the arrows above the plots!)

To **leave a comment** for the author, please follow the link and comment on his blog: ** TC » R**.

R-bloggers.com offers

(This article was first published on ** QuantStrat TradeR » R**, and kindly contributed to R-bloggers)

A few weeks back, after seeing my replication, one of the original authors of the Flexible Asset Allocation paper got in touch with me to tell me to make a slight adjustment to the code, in that rather than remove any negative-momentum securities before performing any ranking, to perform all ranking without taking absolute momentum into account, and only removing negative absolute momentum securities at the very end, after allocating weights.

Here’s the new code:

FAA <- function(prices, monthLookback = 4, weightMom = 1, weightVol = .5, weightCor = .5, riskFreeName = NULL, bestN = 3, stepCorRank = FALSE, stepStartMethod = c("best", "default"), geometric = TRUE, ...) { stepStartMethod <- stepStartMethod[1] if(is.null(riskFreeName)) { prices$zeroes <- 0 riskFreeName <- "zeroes" warning("No risk-free security specified. Recommended to use one of: quandClean('CHRIS/CME_US'), SHY, or VFISX. Using vector of zeroes instead.") } returns <- Return.calculate(prices) monthlyEps <- endpoints(prices, on = "months") riskFreeCol <- grep(riskFreeName, colnames(prices)) tmp <- list() dates <- list() for(i in 2:(length(monthlyEps) - monthLookback)) { #subset data priceData <- prices[monthlyEps[i]:monthlyEps[i+monthLookback],] returnsData <- returns[monthlyEps[i]:monthlyEps[i+monthLookback],] #perform computations momentum <- data.frame(t(t(priceData[nrow(priceData),])/t(priceData[1,]) - 1)) momentum <- momentum[,!is.na(momentum)] #momentum[is.na(momentum)] <- -1 #set any NA momentum to negative 1 to keep R from crashing priceData <- priceData[,names(momentum)] returnsData <- returnsData[,names(momentum)] momRank <- rank(momentum) vols <- data.frame(StdDev(returnsData)) volRank <- rank(-vols) cors <- cor(returnsData, use = "complete.obs") if (stepCorRank) { if(stepStartMethod=="best") { compositeMomVolRanks <- weightMom*momRank + weightVol*volRank maxRank <- compositeMomVolRanks[compositeMomVolRanks==max(compositeMomVolRanks)] corRank <- stepwiseCorRank(corMatrix=cors, startNames = names(maxRank), bestHighestRank = TRUE, ...) } else { corRank <- stepwiseCorRank(corMatrix=cors, bestHighestRank=TRUE, ...) } } else { corRank <- rank(-rowSums(cors)) } totalRank <- rank(weightMom*momRank + weightVol*volRank + weightCor*corRank) upper <- length(names(returnsData)) lower <- max(upper-bestN+1, 1) topNvals <- sort(totalRank, partial=seq(from=upper, to=lower))[c language="(upper:lower)"][/c] #compute weights longs <- totalRank %in% topNvals #invest in ranks length - bestN or higher (in R, rank 1 is lowest) longs[momentum < 0] <- 0 #in previous algorithm, removed momentums < 0, this time, we zero them out at the end. longs <- longs/sum(longs) #equal weight all candidates longs[longs > 1/bestN] <- 1/bestN #in the event that we have fewer than top N invested into, lower weights to 1/top N names(longs) <- names(totalRank) #append removed names (those with momentum < 0) removedZeroes <- rep(0, ncol(returns)-length(longs)) names(removedZeroes) <- names(returns)[!names(returns) %in% names(longs)] longs <- c(longs, removedZeroes) #reorder to be in the same column order as original returns/prices longs <- data.frame(t(longs)) longs <- longs[, names(returns)] #append lists tmp[[i]] <- longs dates[[i]] <- index(returnsData)[nrow(returnsData)] } weights <- do.call(rbind, tmp) dates <- do.call(c, dates) weights <- xts(weights, order.by=as.Date(dates)) weights[, riskFreeCol] <- weights[, riskFreeCol] + 1-rowSums(weights) strategyReturns <- Return.rebalancing(R = returns, weights = weights, geometric = geometric) colnames(strategyReturns) <- paste(monthLookback, weightMom, weightVol, weightCor, sep="_") return(strategyReturns) }

And here are the new results, both with the original configuration, and using the stepwise correlation ranking algorithm introduced by David Varadi:

mutualFunds <- c("VTSMX", #Vanguard Total Stock Market Index "FDIVX", #Fidelity Diversified International Fund "VEIEX", #Vanguard Emerging Markets Stock Index Fund "VFISX", #Vanguard Short-Term Treasury Fund "VBMFX", #Vanguard Total Bond Market Index Fund "QRAAX", #Oppenheimer Commodity Strategy Total Return "VGSIX" #Vanguard REIT Index Fund ) #mid 1997 to end of 2012 getSymbols(mutualFunds, from="1997-06-30", to="2014-10-30") tmp <- list() for(fund in mutualFunds) { tmp[[fund]] <- Ad(get(fund)) } #always use a list hwne intending to cbind/rbind large quantities of objects adPrices <- do.call(cbind, args = tmp) colnames(adPrices) <- gsub(".Adjusted", "", colnames(adPrices)) original <- FAA(adPrices, riskFreeName="VFISX") swc <- FAA(adPrices, riskFreeName="VFISX", stepCorRank = TRUE) originalOld <- FAAreturns(adPrices, riskFreeName="VFISX") swcOld <- FAAreturns(adPrices, riskFreeName="VFISX", stepCorRank=TRUE) all4 <- cbind(original, swc, originalOld, swcOld) names(all4) <- c("original", "swc", "origOld", "swcOld") charts.PerformanceSummary(all4)

> rbind(Return.annualized(all4)*100, + maxDrawdown(all4)*100, + SharpeRatio.annualized(all4)) original swc origOld swcOld Annualized Return 12.795205 14.135997 13.221775 14.037137 Worst Drawdown 11.361801 11.361801 13.082294 13.082294 Annualized Sharpe Ratio (Rf=0%) 1.455302 1.472924 1.377914 1.390025

And the resulting equity curve comparison

Overall, it seems filtering on absolute momentum after applying all weightings using only relative momentum to rank actually improves downside risk profiles ever so slightly compared to removing negative momentum securities ahead of time. In any case, FAAreturns will be the function that removes negative momentum securities ahead of time, and FAA will be the ones that removes them after all else is said and done.

I’ll return to the standard volatility trading agenda soon.

Thanks for reading.

Note: I am a freelance consultant in quantitative analysis on topics related to this blog. If you have contract or full time roles available for proprietary research that could benefit from my skills, please contact me through my LinkedIn here.

To **leave a comment** for the author, please follow the link and comment on his blog: ** QuantStrat TradeR » R**.

R-bloggers.com offers

(This article was first published on ** Revolutions**, and kindly contributed to R-bloggers)

by Tim Winke

PhD student in Demography and Social Sciences in Berlin

*This post has been abstracted from Tim's entry to a contest that Dalia Research is running based on a global smarthpone survey that they are conducting. Tim's entry post is available as is all of the code behind it. - editor*

When people think about Germany, what comes to their mind? Oktoberfest, ok – but Mercedes might be second or BMW or Porsche. German car brands have a solid reputation all over the world, but how popular is each brand in different countries?

There are plenty of survey data out there but hardly anyone collects answers within a couple of days from 6 continents. A new start-up called Dalia Research found a way to use smartphone and tablet networks to conduct surveys. It’s not a separate app but works via thousands of apps where targeted users decide to take part in a survey in exchange for an incentive.

In August 2014, they asked 51 questions to young mobile users in 64 countries including Colombia, Iran and the Ukraine. This is impressive – you have access to opinions of 32 000 people collected within 4 days from all over the world – 500 respondents in each country – about their religion, what they think about the Unites States, about where the EU has global influence or if Qatar should host the 2022 FIFA World Cup, and also: “What is your favorite German car brand?”.

Surprisingly, as the map below shows, BMW seems to be the most popular German car brand – and Volkswagen does not reach the pole position in any country.

The ggplot2 stacked barchart provides even more detail.

To see how I employed dplyr, ggplot2 and rworldmap to construct these plots as well as how to integrate the survey data with world development indicators from the World Bank please have a look at my original post.

To **leave a comment** for the author, please follow the link and comment on his blog: ** Revolutions**.

R-bloggers.com offers

(This article was first published on ** 4D Pie Charts » R**, and kindly contributed to R-bloggers)

Earlier today I saw the hundred bazillionth question about how to use the `paste`

function. My initial response was “take a look at example(paste) to see how it works”.

Then I looked at `example(paste)`

, and it turns out that it’s not very good at all. There isn’t even an example of how to use the `collapse`

argument. Considering that `paste`

is one of the first functions that beginners come across, as well as being a little bit tricky (getting to understand the difference between the `sep`

and `collapse`

arguments takes a bit of thinking about when you are new), this seems like a big oversight.

I’ve submitted this as a bug, with a suggested improvement to the examples. Fingers crossed that R-core will accept the update, or something like it.

It got me thinking though, how many other base functions could do with better examples? I had a quick look at some common functions that beginners seems to get confused with, and the following all have fairly bad example sections:

In base: `browser`

, `get`

, `seq`

In stats: `formula`

, `lm`

, `runif`

, `t.test`

In graphics: `plot`

In utils: `download.file`

, `read.table`

If you have half an hour spare, have a go at writing a better example page for one of these functions, or any other function in the base distribution, then submit it to the bug tracker. (If you aren’t sure that your examples are good enough, or you need advice, try posting what you have on r-devel before submitting a bug report. Dealing with bug reports takes up valuable R-core time, so you need to be sure of quality first.)

This seems like a really easy way to make R more accessible for beginners.

Tagged: beginners, documentation, r, r-core

To **leave a comment** for the author, please follow the link and comment on his blog: ** 4D Pie Charts » R**.

R-bloggers.com offers