# Build a path probability tree for journeys through a website

I'm currently doing analysis on a website which requires that I create a decision tree diagram showing the likely route that people take whenever they arrive on the website. I am dealing with a data.frame which shows the paths of all customers to the site, starting from the homepage. For example, a customer could take the following path:

Homepage - pg 1
Kitchen Items page - pg 2
Pots and Pans page - pg 3


so this customer would have a 3 page journey. What I want to try to do in R is combine all customers paths and thereby assign a probability to a customer following a certain path on the site. For example, if I were to examine all paths I could find that 34% of people who arrive on the Homepage go onto the 'Kitchen items page'. Does R have this facility?

I have looked up different methods through the rpart and partykit packages but they didn't seem to be of any help.

Any steer in the right direction for this is very much appreciated!

• i don't know much about this area, but the igraph package appears to be quite comprehensive. – richiemorrisroe Jan 28 '13 at 19:17
• yup, igraph is the way to go for visualization. You have to compute the transition probabilities beforehand on your own. In general, I recommed to take a look at Markov Chains – steffen Jan 29 '13 at 12:40
• Can you post some sample data? It'd help us understand the situation better. – curious_cat Mar 3 '13 at 6:23

Isn't one way to start, is to have a $n \times n$ matrix (say $M_{n \times n}$) where $n$ is the number of pages. Then based on your raw data increment matrix element $M_{rc}$ by one whenever you have a user hop from page $r$ to page $c$. That gets you the transition probabilities.

Your first question is already answered by this: "What percent of users on homepage (say page 1) travel next to, say, Kitchen Items (say page 2)?"

$\frac{M_{12}}{ \sum_c M_{1c}}$

Or is this too simplistic?

• No this is right. From here though - is there a way to graph each of this dynamically into a tree in R? If not, is there another tool I could use? – nellington Mar 11 '13 at 12:43
• @nellington: What sort of tree do you have in mind? – curious_cat Mar 11 '13 at 13:22
• Preferably a probability tree, with the root node being the homepage (but if I can change the root node, to another page on the site - that would be a great feature), each branch from the root node would represent the next page visited after the root node. Ideally, each branch would have a % probability attached. It would be dynamic in the sense that I should be able to expand and contract on each sub-node. Do you know of any visualisation software which could handle this? – nellington Mar 11 '13 at 13:38
• @nellington: For visualising purely, you could try graphviz. That tree will be a directed graph and there's plenty of graphviz-oriented tools to handle that. – curious_cat Apr 2 '13 at 7:00

It looks like you are trying to recreate the PageRank algorithm of Google. Most of the PageRank algorithm was developed using Markov Chains. You can find a lot of mentions of developing PageRank methods in R.

igraph.sourceforge.net/doc/R/page.rank.htm

• As far as I see, this is not about pagerank. IMHO, the only overlap is that user paths will most likely correlate to site design (links), but that's it. Aside, the provided link is not working. – steffen Jan 31 '13 at 12:01
• I think I found the link, it probably was on igraph's webpage at some point because it appears to use igraph heavily. stat.berkeley.edu/users/vigre/undergrad/reports/… – geneorama Jan 31 '13 at 19:51
• Oh, I see... and page.rank is a function in igraph. Some documentation: link1 link2 link3 – geneorama Jan 31 '13 at 19:58
• After briefly skimming the first report, I actually think this is a pretty good answer, and I up-voted it (although it could have used some elaboration!). The page rank functionality seems to be the answer. – geneorama Jan 31 '13 at 20:02

From what I see here, I agree that igraphs / Markov Chains is probably the way to go, however you could definitely use rpart and/or the partykit.

It's hard for me to give a simple answer with your limited example, but I can explain generally how you would do it.

You want look at where all your users had been, and summarize that into a string for example

"Home / product4 / product3 / product4 / buynow"
"Home / product3 / product4"


You could then segment your users into categories, say ones who ended up in the "buy now" page, and ones who didn't. Then you could simply start predicting on that terminal result. In this example, maybe you would find out that people who did the most comparison shop did / didn't buy something.

You could also make more variables, like "what was the page before the buynow page" "how many pages did they visit before buying something" or "when did they create their first account", and you could add those metrics to your analysis.

There are a lot of different ways you could go, and this begins to answer different questions, but my point is that you could use the trees and for some problems it might be a faster and simpler route to insight.

By the way, you would need to make non-numeric variables factors by using factor or as.factor, if you're going to use party. Party has some nice vignettes to get you started.

• To be honest, I want to optimise the homepage, so predicting where people go from the homepage to page 2 and then page 2 to the third page is the most important section of data I would like. Terminal page isn't of much interest. I have page urls and page numbers in the journey so transition probabilites seems like a way to do it. Despite this, it seems a bit manual and I though R might be able to provide a more iterative solution... – nellington Feb 13 '13 at 10:22
• After rereading your question and last comment, I think that you simply want a table of what people do from the home page. (to start) – geneorama Feb 13 '13 at 14:50
• What people do from the homepage and the page after the homepage is most important, but being able to concatenate all users data in r and assign probabilities is where it's most tricky. Maybe excel is the way to go? I'm going down the vertices/edges route in igraph but it seems to be causing more harm than help. – nellington Feb 13 '13 at 16:01
• I added some contact info to my profile. Maybe we could talk offline? – geneorama Feb 13 '13 at 16:15
• that would be very helpful thanks - mail sent – nellington Feb 13 '13 at 17:23