Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function(x, name = FALSE) {
- # get referenced object name
- if(name == FALSE){
- name <- deparse(substitute(x))
- }
- cat( name , "\n")
- # create page
- if(is.numeric(x)) {
- par(mfrow=c(3,2), oma=c(0,0,4,0))
- } else {
- par(mfrow=c(1,1), oma=c(0,0,4,0))
- }
- oldpar <- par(mar=c(0,0,0,0), mgp=c(0,0,0));
- par(mar=c(oldpar$mar[1],oldpar$mar[2],oldpar$mar[3]*0.5,oldpar$mar[4]))
- par(mgp=c(oldpar$mgp[1]*0.6,oldpar$mgp[2]*0.6,oldpar$mgp[3]))
- # create rug plot
- plot(table(x), main = "Rug Plot", xlab = name, ylab = "Frequency")
- # create density plot
- if(is.numeric(x)) densityplot <- plot(density(x), main = "Density Plot", xlab = name )
- # create base histogram
- if(is.numeric(x)) {
- histogram <- hist(x, main = "Histogram", xlab = name )
- # calculate normal distribution line
- xfit<-seq(min(x),max(x),length=40)
- yfit<-dnorm(xfit,mean=mean(x),sd=sd(x)) * diff(histogram$mids[1:2])*length(x)
- # draw normal distribution line
- lines(xfit, yfit, col="blue", lwd=1)
- }
- # create QQ plot
- if(is.numeric(x)) { qqnorm(x, main = "QQ Plot" ); qqline(x) }
- # create boxplot
- if(is.numeric(x)) boxplot(x, horizontal = TRUE, main = "Box Plot", xlab = name )
- # output frequency table to console
- freq_table <- transform(data.frame(table(x)),percentage_column=Freq/sum(Freq)*100)
- if( nrow(freq_table) < 10 || !is.numeric(x) ) {
- cat('\n')
- if(!is.numeric(x)) {
- freq_table <- freq_table[ order(-freq_table[,3]), ]
- }
- c('value', 'n', '%') -> colnames(freq_table)
- print(freq_table)
- cat('\n')
- }
- # output mean, sd, median, (interquartile) range as text to console and plot, with max. 6 significant digits
- if(is.numeric(x)) {
- cat( sprintf( "mean: %g S: %g" , mean(x) , sd(x) ) , "\n" )
- 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" )
- par(mar=c(0,0,oldpar$mar[3]*0.5,0))
- plot(0:10, 0:10, main = "Descriptive Values", type = "n", axes = FALSE, xlab = "", ylab = "")
- 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))
- text(0, 7.8, sprintf( "mean: %g" , mean(x) ), family = "mono", adj = c(0,0))
- text(0, 6.8, sprintf( "S: %g" , sd(x) ), family = "mono", adj = c(0,0))
- text(0, 4.8, sprintf( "median: %g" , median(x) ), family = "mono", adj = c(0,0))
- 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))
- 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))
- text(0, 0.8, substitute(italic("max significance: 6 digits")), adj = c(0,0))
- }
- # insert header text
- string = paste0(" (n=", length(x), ")\n")
- mtext( paste0( name, string ), side = 3, outer = TRUE, font=2)
- # reset par
- par(oldpar)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement