Advertisement
Guest User

castogram(x)

a guest
Mar 17th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 3.06 KB | None | 0 0
  1. function(x, name = FALSE) {
  2.  
  3.   # get referenced object name
  4.   if(name == FALSE){
  5.     name <- deparse(substitute(x))
  6.   }
  7.   cat( name , "\n")
  8.  
  9.   # create page
  10.   if(is.numeric(x)) {
  11.     par(mfrow=c(3,2), oma=c(0,0,4,0))
  12.   } else {
  13.     par(mfrow=c(1,1), oma=c(0,0,4,0))
  14.   }
  15.   oldpar <- par(mar=c(0,0,0,0), mgp=c(0,0,0));
  16.   par(mar=c(oldpar$mar[1],oldpar$mar[2],oldpar$mar[3]*0.5,oldpar$mar[4]))
  17.   par(mgp=c(oldpar$mgp[1]*0.6,oldpar$mgp[2]*0.6,oldpar$mgp[3]))
  18.  
  19.   # create rug plot
  20.   plot(table(x), main = "Rug Plot", xlab = name, ylab = "Frequency")
  21.  
  22.   # create density plot
  23.   if(is.numeric(x)) densityplot <- plot(density(x), main = "Density Plot", xlab = name )
  24.  
  25.   # create base histogram
  26.   if(is.numeric(x)) {
  27.     histogram <- hist(x, main = "Histogram", xlab = name )
  28.     # calculate normal distribution line
  29.     xfit<-seq(min(x),max(x),length=40)
  30.     yfit<-dnorm(xfit,mean=mean(x),sd=sd(x)) * diff(histogram$mids[1:2])*length(x)
  31.     # draw normal distribution line
  32.     lines(xfit, yfit, col="blue", lwd=1)
  33.   }
  34.  
  35.   # create QQ plot
  36.   if(is.numeric(x)) { qqnorm(x, main = "QQ Plot" ); qqline(x) }
  37.  
  38.   # create boxplot
  39.   if(is.numeric(x)) boxplot(x, horizontal = TRUE, main = "Box Plot", xlab = name )
  40.  
  41.   # output frequency table to console
  42.   freq_table <- transform(data.frame(table(x)),percentage_column=Freq/sum(Freq)*100)
  43.   if( nrow(freq_table) < 10 || !is.numeric(x) ) {
  44.     cat('\n')
  45.     if(!is.numeric(x)) {
  46.       freq_table <- freq_table[ order(-freq_table[,3]), ]
  47.     }
  48.     c('value', 'n', '%') -> colnames(freq_table)
  49.     print(freq_table)
  50.     cat('\n')
  51.   }
  52.  
  53.   # output mean, sd, median, (interquartile) range as text to console and plot, with max. 6 significant digits
  54.   if(is.numeric(x)) {
  55.     cat( sprintf( "mean: %g S: %g" , mean(x) , sd(x) ) , "\n" )
  56.     cat( sprintf( "median: %g IQrange: %g - %g (%g wide) range: %g - %g (%g wide)" , median(x) , quantile(x, probs = .25) , quantile(x, probs = .75) , iqr(x) , range(x)[1] , range(x)[2] , range(x)[2] - range(x)[1] ) , "\n" )
  57.  
  58.     par(mar=c(0,0,oldpar$mar[3]*0.5,0))
  59.     plot(0:10, 0:10, main = "Descriptive Values", type = "n", axes = FALSE, xlab = "", ylab = "")
  60.     text(0, 9.8, sprintf("mean = %.1f S,  Δmean-median: %.1f S", mean(x)/sd(x), abs(mean(x)-median(x))/sd(x) ), adj = c(0,0))
  61.     text(0, 7.8, sprintf( "mean:   %g" , mean(x) ), family = "mono", adj = c(0,0))
  62.     text(0, 6.8, sprintf( "S:      %g" , sd(x) ), family = "mono", adj = c(0,0))
  63.     text(0, 4.8, sprintf( "median: %g" , median(x) ), family = "mono", adj = c(0,0))
  64.     text(0, 3.8, sprintf( "IQR:    %g~%g (%g wide)" , quantile(x, probs = .25) , quantile(x, probs = .75) , iqr(x) ), family = "mono", adj = c(0,0))
  65.     text(0, 2.8, sprintf( "range:  %g~%g (%g wide)" , range(x)[1] , range(x)[2] , range(x)[2] - range(x)[1] ), family = "mono", adj = c(0,0))
  66.     text(0, 0.8, substitute(italic("max significance: 6 digits")), adj = c(0,0))
  67.   }
  68.  
  69.   # insert header text
  70.   string = paste0(" (n=", length(x), ")\n")
  71.   mtext( paste0( name, string ), side = 3, outer = TRUE, font=2)
  72.  
  73.   # reset par
  74.   par(oldpar)
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement