Showing posts with label R. Show all posts
Showing posts with label R. Show all posts

Saturday, November 26, 2011

Analytics using R: Most active in my Twitter list

I follow some 80 odd people/ news sources on my twitter account. For a while I wondered which of these sources are most active on twitter. I picked a simple metric '# of status messages posted to twitter' as the measure of activity. Using R I quickly wrote a program to generate my top 10 most active twitter sources.

Here is the bar plot of the result
As expected news sources dominate the list. Among individuals "Michael Hyatt" and "Jurgen Appelo" are most active. 

If you are interested in 'R', here is the code to extract this report:

## Prerequisite: Install twitteR package 'install.packages(twitteR)
## load twitteR package
library(twitteR)

##get handle to a twitteR user object (in this case for user d_lalit
tuser <- getUser('d_lalit')

##get list of friends of d_lalit
tfriends <- userFriends(tuser)

##create an array to store the name and number of status messages for each friend
friendsCount <- length(tfriends)
friendsName <- character(friendsCount)
friendsMsgCount <- numeric(friendsCount)

for (i in 1:friendsCount) {
  friendsName[i] <- tfriends[[i]]$screenName
  friendsMsgCount[i] <- as.numeric(tfriends[[i]]$statusesCount)
}

## prepare a sortedlist and extract top 10 values from the list
sortedlist <- sort(friendsMsgCount, index.return = TRUE, decreasing=TRUE)
top10friendsName <- character(10)
top10friendsMsgCount <- numeric(10)

for (i in 1:10) {
  top10friendsName[i] <- friendsName[sortedlist$ix[[i]]] ## index is stored under ix
  top10friendsMsgCount[i] <- as.numeric(sortedlist$x[[i]])
}

## plot the chart
barplot(top10friendsMsgCount, width = 0.25, names.arg = top10friendsName, horiz=FALSE, main="Twitter friends by activity count", ylab="Number of status messages", xlab="twitter friends", space=0.2, density=50, angle=45, cex.names=0.7) 
  
I realize the code is not optimally written. Any suggestions refine the code will be appreciated.

Update: 11/29/2011
In the latest version of twitteR package, the method userFriends() has been deprecated. You may replace line#9 in the above code as with the code given below:

tfriends <- tuser$getFriends()

Monday, May 16, 2011

My first 'R' plot

Started learning 'R'.
My first attempt was to plot data from Forbes 1000 list (refer to the exercise posted by Prasoon sharma)

Here is a bubble chart showing Forbes top 25 companies by Market Capitalization

Source code:
## read the csv file
FORBES.DF <- read.csv("forbes2000list_for_2011.csv")

## assign titles
names(FORBES.DF)<- c("Rank", "Company", "Country", "Industry", "Sales", "Profits", "Assets", "MarketCap")

## create a smaller vector
Forbes100ByMC <- FORBES.DF[order(-FORBES.DF$MarketCap),][1:100,]
Forbes25 <- Forbes100ByMC[1:25, ]

## plot the bubble chart using 'symbols'
radius <- sqrt(Forbes25$MarketCap/pi)
sales <- as.numeric(as.character(Forbes25$Sales))
profits <- as.numeric(as.character(Forbes25$Profits))

symbols(sales, profits, circles=radius, inches=0.9, fg="white", bg="light blue", xlab="Sales($'Billions)", ylab="Profits($'Billions)", main="Forbes 25 By Market Capitalization", xlim=c(min(range(sales))-50, max(range(sales))+50), ylim=c(min(range(profits))-2, max(range(profits))+2))
## print the names of companies
text(sales, profits, Forbes25$Company, cex=0.6, col="dark red") 
 
Any feedback toward writting better 'R' code is welcome.

Wednesday, March 23, 2011

jStat: Advanced Statistics using Javascript

While 'R' is getting enterprise ready, it's no longer the only open source option for advanced statistical programming. jStat.js is the new kid on the block.

Things in favor of jStat:
  • Based on Javascript, jQuery - future is assured
  • Light-weight
  • Ability to visualize data using flot (jQuery plugin)
Still no match for 'R' yet (complete API documentation is yet to come out), but certainly a bright prospect in application which require basic statistical analysis and data visualization (where 'R' is an overkill).

Possibilities  with jStat.js - check this out


Thinking out loud..
Node.js + jStat.js ...=..=!=..=... RIP 'R' ??