# Pattern Clustering

I have data of daily time spent in studying for around 2000 students. I need to make clusters based on the pattern of hour spent in a week, not by average hour spent.For example some students are studying for 5 hours a day during weekdays & in weekend it is 7 hours,for some students 2 hours in weekdays & weekend it is 4 hours & for some students 0 hour in weekend & 6-8 hours in weekdays.I want to cluster these patterns along with some other macro economic variables. Can you please suggest me the logic for pattern clustering or link to read about pattern clustering?

• Simply searching for an R function is off-topic for CV (see ou help page). However, as written this Q will be off-topic on Stack Overflow as well. What you need is an overview of clustering. For starters, try reading through some of the threads categorized under the clustering tag. – gung - Reinstate Monica Dec 26 '13 at 16:55
• Sorry My fault.I didn't check the help page. I will edit my post – R Learner Dec 26 '13 at 17:07

I don't think that you need a special "pattern clustering" algorithm to handle your problem. All you need to do is mold your existing data into a format that can readily be processed by conventional clustering algorithms.

Conventional clustering algorithms such a kmeans require an input data table containg $N$ columns. Each column can be thought of as representing a point along an axis which is orthogonal to all of the others. The cluster IDs are then assigned by searching for groups of points which are physically proximate to one another, according to some distance metric, using a cluster assignment process which is particular to each type of clustering algorithm.

To perform a basic clustering analysis, you really only need to decide upon three things, 1.) what should the input data table look like, 2.) what clustering algorithm would you like to use, and 3.) what distance metric should you use?

For your input data table, I'd suggest that you start by creating one column to contain the number of weekday hours that students spend studying, and another for the number of weekend hours, and then create however many additional columns you need in order to hold all of the macroeconomic variables. Later, if you want to iterate with a more sophisticated analysis, you can subdivide the single "weekend" column to make two separate columns, one each for Saturday and Sunday, or you may divide the weekday hours into mornings, afternoons and evenings, etc.

For the clustering algorithm itself, I'd suggest you start with the kmeans algorithm that has already been mentioned; it's a very simple method that is widely used and recognized, and thus it makes a very defensible choice as an initial default. R includes this implementation of kmeans in the basic stats library. Later, if you'd like to try other algorithms such as xmeans or expectation maximization, those are other more sophisticated options that are available as well.

For the distance metric, I'd suggest starting with $N$-dimensional Euclidian distance unless you have some special insight or other reason to expect that another option might work better for this class of problem instead.

If you choose to use Euclidian distance, you should be aware that Euclidian distance by default assumes that all columns in your input data table have the same units. This is clearly not the case with your data set--the first two columns will have units of hours, while the macroeconomic variables will likely be denominated in currency units such as euros or dollars. For this reason, it is customary to normalize each column separately so that each one varies across an approximately equal range or scale. So, for example, you might divide the column containing hours of weekday study by 120 hours (5 * 24 = total number of weekday hours) to get a fraction of weekday time spent studying, and similarly with the weekend, except that you would instead divide by 48 hours. If one of the macroeconomic variables were, say, parent's income, you might want to divide all of the numbers in that column by, say, the median value for the column. The precise methodology that you choose for normalization isn't really all that important, and it's O.K. to have one column whose values vary between [0, 1] while another column varies between [0, 2]. Mainly what you want to avoid is a grossly mismatched situation where the values in one column in their native units vary across an extremely narrow range such as [0.4, 0.5], while another column varies across an extremely wide range such as [0, 1000000].

• Thank you stachyra for your comments. Yes you are right. I created 7 columns for 7 days & one id column,identifying student.I used this as input data for k-means clustering & able to cluster by patterns – R Learner Dec 29 '13 at 4:40
• @R Learner: so essentially you're saying that my answer appears to have been correct? In that case, if nobody else manages to come up with an even better answer within a reasonable period of time, please remember to eventually mark it as "accepted". – stachyra Dec 29 '13 at 5:16
• Done. Actually that day I tried to select your answer as useful but not able to do because of less than 15 reputation – R Learner Dec 30 '13 at 6:13