I have written a function to emulate minitab's QQ plotting output (with SW test and AD test results on the graph): mtab.norm<-function(x) { library(nortest) library(lattice) x<-as.numeric(x) x<-as.vector(x) plot.ht<-4.6 plot.wd<-4.6 pt.ht=plot.ht/5 txt.sz<-(plot.ht/7.5) X11(width=plot.wd, height=plot.ht, bg='gray96') qqplot(x, pch=16, cex=pt.ht, col=palette()[c(1,2)], xlab=paste(colnames(x)), ylab='Normal Quantile-Quantile Plot', main='') grid(grid(col = "darkgray")) st.x<-shapiro.test(x) mtext(paste(st.x$method, "\np=",st.x$p.value,"\n W=",st.x$statistic, line=1, cex=txt.sz, adj=0)) adt.x<-ad.test(x) mtext(paste(adt.x$method, "\np=",adt.x$p.value, "\n W=", adt.x$statistic, line=1, cex=txt.sz, adj=1))} mtab.matrix<-function(z){apply(z, 2, mtab.norm)} However, I get the following message when I try to apply it:> ab<-rnorm(1:100, 2) > aa<-log(ab)Warning message: In log(ab) : NaNs produced> ac<-(ab^2) > df<-data.frame(aa,ab,ac)> mtab.matrix(df)Error in sort(y) : argument "y" is missing, with no default> mtab.norm(aa)Error in sort(y) : argument "y" is missing, with no default I'm doing something syntactically stupid. I can't figure out what. Thanks for your help -- View this message in context: http://r.789695.n4.nabble.com/adding-statistical-output-to-a-plot-tp2235565p2235565.html Sent from the R help mailing list archive at Nabble.com.
Two things: 1) doing log on rnorm, will get you NaN's because you are doing log on a negative number (some of the time). 2) Running> traceback()5: sort(y) 4: qqplot(x, pch = 16, cex = pt.ht, col = palette()[c(1, 2)], xlab paste(colnames(x)), ylab = "Normal Quantile-Quantile Plot", main = "") 3: FUN(newX[, i], ...) 2: apply(z, 2, mtab.norm) 1: mtab.matrix(df) Shows that you are using qqplot wrongly somehow. Using ?qqplot Shows that you should either use qqnorm (not qqplot) Or have another argument for another distribution. Let me know if it works :) Best, Tal ----------------Contact Details:------------------------------------------------------- Contact me: Tal.Galili@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) ---------------------------------------------------------------------------------------------- On Sat, May 29, 2010 at 12:39 PM, dunner <ross.dunne@tcd.ie> wrote:> > I have written a function to emulate minitab's QQ plotting output (with SW > test and AD test results on the graph): > > mtab.norm<-function(x) > { library(nortest) > library(lattice) > x<-as.numeric(x) > x<-as.vector(x) > plot.ht<-4.6 > plot.wd<-4.6 > pt.ht=plot.ht/5 > txt.sz<-(plot.ht/7.5) > X11(width=plot.wd, height=plot.ht, bg='gray96') > qqplot(x, pch=16, cex=pt.ht, col=palette()[c(1,2)], > xlab=paste(colnames(x)), > ylab='Normal Quantile-Quantile Plot', main='') > > grid(grid(col = "darkgray")) > st.x<-shapiro.test(x) > > mtext(paste(st.x$method, "\np=",st.x$p.value,"\n W=",st.x$statistic, > line=1, > cex=txt.sz, adj=0)) > > adt.x<-ad.test(x) > mtext(paste(adt.x$method, "\np=",adt.x$p.value, "\n W=", adt.x$statistic, > line=1, cex=txt.sz, adj=1))} > mtab.matrix<-function(z){apply(z, 2, mtab.norm)} > > However, I get the following message when I try to apply it: > > > ab<-rnorm(1:100, 2) > > aa<-log(ab) > Warning message: > In log(ab) : NaNs produced > > ac<-(ab^2) > > df<-data.frame(aa,ab,ac) > > > mtab.matrix(df) > Error in sort(y) : argument "y" is missing, with no default > > > mtab.norm(aa) > Error in sort(y) : argument "y" is missing, with no default > > I'm doing something syntactically stupid. I can't figure out what. > > Thanks for your help > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/adding-statistical-output-to-a-plot-tp2235565p2235565.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
On 05/29/2010 07:39 PM, dunner wrote:> > I have written a function to emulate minitab's QQ plotting output (with SW > test and AD test results on the graph): > ... > However, I get the following message when I try to apply it: > >> ab<-rnorm(1:100, 2) >> aa<-log(ab) > Warning message: > In log(ab) : NaNs producedAlmost certainly negative numbers in ab - try: ab<-abs(ab) to flip these to positive or use a larger mean>> ac<-(ab^2) >> df<-data.frame(aa,ab,ac) > >> mtab.matrix(df) > Error in sort(y) : argument "y" is missing, with no default > >> mtab.norm(aa) > Error in sort(y) : argument "y" is missing, with no default >These errors look like they are happening in qqplot Jim
Thanks to both of you, I used the below code instead, which has been cleaned and pruned a little. It produces a trellis of qq plots with the proper annotations as per Minitab from a dataframe passed to mtab.matrix(). However, I can't get individual variable names (these are column names in the passed dataframe) to annotate the plots. Perhaps "apply" simply doesn't deal with the colname and I have to try a completely different method? Or will apply pass enough information to the included function (mtab.norm) to allow me to annotate the plot? Again, I'm probably missing something banal and wholly obvious. mtab.norm<-function(x){ library(nortest) library(lattice) x<-as.numeric(x) x<-as.vector(x) plot.ht<-4.6 plot.wd<-4.6 pt.ht=plot.ht/7 txt.sz<-(plot.ht/5) qqnorm(x, pch=16, cex=pt.ht, col="darkorange", xlab=paste(colnames(x)), ylab='', main='') qqline(x, col="black", lty=2) grid(col = "darkgrey", lty=2) st.x<-shapiro.test(x) mtext(paste("Shapiro-Wilks", "\n p =",signif(st.x$p.value,digits=4),"\n W =",signif(st.x$statistic,digits=4)), line=1, cex=txt.sz, adj=0) adt.x<-ad.test(x) mtext(paste("Anderson-Darling", "\n p =",signif(adt.x$p.value,digits=4), "\n W =", signif(adt.x$statistic,digits=4)), line=1, cex=txt.sz, adj=1)} mtab.matrix<-function(z){ pdf(file=paste(colnames(z)[1],".pdf"), paper="a4r", width=0, height=0) par(mfcol=c(3,3), mar=c(5.1,4.1,5.1,2.1)) par(bg="white") apply(z, 2, mtab.norm) dev.off()} Thanks again for all your help. I am obviously a relative beginner, but an enthusiast! Dr. Ross Dunne, HRB Clinical Research Fellow, Dept. of Psychiatry, Trinity College Dublin. -- View this message in context: http://r.789695.n4.nabble.com/adding-statistical-output-to-a-plot-tp2235565p2235629.html Sent from the R help mailing list archive at Nabble.com.