if(interactive()){
# initialize connection to Lahman baseball database in Aster
conn = odbcDriverConnect(connection="driver={Aster ODBC Driver};
server=<dbhost>;port=2406;database=<dbname>;uid=<user>;pwd=<pw>")
km = computeKmeans(conn, "batting", centers=5, iterMax = 25,
aggregates = c("COUNT(*) cnt", "AVG(g) avg_g", "AVG(r) avg_r", "AVG(h) avg_h"),
id="playerid || '-' || stint || '-' || teamid || '-' || yearid",
include=c('g','r','h'), scaledTableName='kmeans_test_scaled',
centroidTableName='kmeans_test_centroids',
where="yearid > 2000")
createCentroidPlot(km)
# As clusters fluctuate from run to run cluster names may be inconsistent
createCentroidPlot(km, format="bar_dodge",
clusters=c(Stars=3, Reserve=4), dims=c('g','h'))
createCentroidPlot(km, format="heatmap", coordFlip=TRUE)
}
Run the code above in your browser using DataLab