[Traminer-users] loop for plotting quality statistics from wcKMedRange
Reynolds, Jeremy E
reyno113 at purdue.edu
Mon Oct 10 20:27:07 CEST 2016
Hi Thomas,
Thank you again for your help. It appears, however, that I am still stuck. When I got back to my computer to try your suggested solutions, I did not get the expected results. To help with the diagnosis of the problem, I have written an example below that can be run to reproduce the problems. My attempts at using loops are at the end of the code .
Jeremy
library(TraMineR)
library(WeightedCluster)
data(mvad)
mvad.alphabet <- c("employment", "FE", "HE", "joblessness", "school", "training")
mvad.labels <- c("Employment", "Further Education", "Higher Education", "Joblessness", "School", "Training")
mvad.scodes <- c("EM", "FE", "HE", "JL", "SC", "TR")
mvadseq <- seqdef(mvad[, 17:86], alphabet = mvad.alphabet, states = mvad.scodes, labels = mvad.labels, weights = mvad$weight, xtstep = 6)
#Defining the custom cost matrix
subm.custom <- matrix(
c(0, 1, 1, 2, 1, 1,
1, 0, 1, 2, 1, 2,
1, 1, 0, 3, 1, 2,
2, 2, 3, 0, 3, 1,
1, 1, 1, 3, 0, 2,
1, 2, 2, 1, 2, 0),
nrow = 6, ncol = 6, byrow = TRUE)
#Compute the OM dissimilarities
mvaddist1.0 <- seqdist(mvadseq, method = "OM", indel = 1.0, sm = subm.custom)
mvaddist1.5 <- seqdist(mvadseq, method = "OM", indel = 1.5, sm = subm.custom)
mvaddist2.0 <- seqdist(mvadseq, method = "OM", indel = 2.0, sm = subm.custom)
#obtain a series of cluster solutions
pamclust1.0 <- wcKMedRange(mvaddist1.0, kvals = 2:10, weights = mvad$weight)
pamclust1.5 <- wcKMedRange(mvaddist1.5, kvals = 2:10, weights = mvad$weight)
pamclust2.0 <- wcKMedRange(mvaddist2.0, kvals = 2:10, weights = mvad$weight)
#print statistics for the various solutions
round(pamclust1.0$stats[,c("ASW", "HC", "R2", "PBC", "HG")],3)
round(pamclust1.5$stats[,c("ASW", "HC", "R2", "PBC", "HG")],3)
round(pamclust2.0$stats[,c("ASW", "HC", "R2", "PBC", "HG")],3)
#plot the statistics for the various solutions
plot(pamclust1.0, stat=c("ASW","HC", "R2", "PBC", "HG"), legendpos="topright", norm="zscore")
title(main="pamclust1.0")
plot(pamclust1.5, stat=c("ASW","HC", "R2", "PBC", "HG"), legendpos="topright", norm="zscore")
title(main="pamclust1.5")
plot(pamclust2.0, stat=c("ASW","HC", "R2", "PBC", "HG"), legendpos="topright", norm="zscore")
title(main="pamclust2.0")
############
#Many of the steps above involve a good bit of repetition.
#I would rather do some of them with a loop, but all of my attempts below fail
############
#print statistics using a loop - attempt 1
indels1 <- list(pamclust1.0$stats, pamclust1.5$stats, pamclust2.0$stats)
for (i in indels1){
round(i[,c("ASW", "HC", "R2", "PBC", "HG")],3)
}
#print statistics using a loop - attempt 2
indels2 <- list(1.0, 1.5, 2.0)
for (i in indels2){
round(get(paste0("pamclust",i,"$stats"))[,c("ASW", "HC", "R2", "PBC", "HG")],3)
}
#plot statistics using a loop - attempt 1
indels3 <- list(pamclust1.0, pamclust1.5, pamclust2.0)
for (i in indels3){
plot(i, stat=c("ASW","HC", "R2", "PBC", "HG"), legendpos="topright", norm="zscore")
title(main=i)
}
From: traminer-users-bounces at lists.r-forge.r-project.org [mailto:traminer-users-bounces at lists.r-forge.r-project.org] On Behalf Of thomas collas
Sent: Saturday, October 8, 2016 8:29 AM
To: Users questions <traminer-users at lists.r-forge.r-project.org>
Subject: Re: [Traminer-users] loop for plotting quality statistics from wcKMedRange
Sure, you can use the pdf() function before the par() one. As quartz(), pdf() allows to define the dimensions of the window. Instead of par(), the layout() function is great too to divide the window into several plots. The pdf() file is produced while R meets the dev.off() line after the commands generating the plots, the axes, and so on.
pdf(file="working_directory/plots.pdf", height=, etc.)
par(mfrow=c(1,2))
for (i in stats) {
plot(i, stat=c("ASW","HC", "R2", "PBC", "HG"), legendpos="topright", norm="zscore")
}
dev.off()
Best,
thomas
2016-10-08 14:09 GMT+02:00 Reynolds, Jeremy E <reyno113 at purdue.edu<mailto:reyno113 at purdue.edu>>:
Thank you Thomas! It is amazing how many things a novice user can mess up in just a few lines. Loops and the apply functions seem to be a perenniaI source of confusion in R.
I was hoping to save the plots in a single file (perhaps PDF?). Do you have any additional tips for that?
Thanks
Jeremy
Sent from my iPhone
On Oct 8, 2016, at 2:10 AM, thomas collas <thomas.collas at gmail.com<mailto:thomas.collas at gmail.com>> wrote:
Dear Jeremy,
That should work if you change 3 things :
- the first problem is your stats object, which is a character vector, not a list of objects, you can't plot a character string.
- the second problem is the "x" in the plot function. If i is a component and not an attribute of stats, you can't index stats with i.
- the third smaller problem is that you have to ask R to print two plots. If it is only for visualization, you can use the mfrow argument in the par() function.
After having created the two objects you want to plot, this code should work :
stats <- list(pamomf2i5q.w, pamwardomf2i5q.w)
par(mfrow=c(1,2))
for (i in stats) {
plot(i, stat=c("ASW","HC", "R2", "PBC", "HG"), legendpos="topright", norm="zscore")
}
Best,
thomas
2016-10-08 4:41 GMT+02:00 Reynolds, Jeremy E <reyno113 at purdue.edu<mailto:reyno113 at purdue.edu>>:
Dear TraMineR Users,
I would like to loop through many sets of clustering results produced with wcKMedRange and produce a plot for each one. Without the loop, the code would look like this:
plot(pamomf2i5q.w, stat=c("ASWw","HC", "R2", "PBC", "HG"), legendpos="topright", norm="zscore")
plot(pamwardomf2i5q.w, stat=c("ASWw","HC", "R2", "PBC", "HG"), legendpos="topright", norm="zscore")
When I try to put this in a loop, I get the error messages below suggesting that R is expecting a y value in the plot command. Is there something wrong with my loop that is causing this problem?
Thanks,
Jeremy
> stats <- c("pamomf2i5q.w", "pamwardomf2i5q.w")
> for (i in unique(stats)) {
+ plot(stats[i], stat=c("ASW","HC", "R2", "PBC", "HG"), legendpos="topright", norm="zscore")
+ }
<image001.png> Show Traceback
Rerun with Debug
Error in plot.window(...) : need finite 'ylim' values
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
3: In plot.window(...) : "stat" is not a graphical parameter
4: In plot.window(...) : "legendpos" is not a graphical parameter
5: In plot.window(...) : "norm" is not a graphical parameter
_______________________________________________
Traminer-users mailing list
Traminer-users at lists.r-forge.r-project.org<mailto:Traminer-users at lists.r-forge.r-project.org>
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/traminer-users
_______________________________________________
Traminer-users mailing list
Traminer-users at lists.r-forge.r-project.org<mailto:Traminer-users at lists.r-forge.r-project.org>
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/traminer-users
_______________________________________________
Traminer-users mailing list
Traminer-users at lists.r-forge.r-project.org<mailto:Traminer-users at lists.r-forge.r-project.org>
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/traminer-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/traminer-users/attachments/20161010/c64c5126/attachment-0001.html>
More information about the Traminer-users
mailing list