Skip to main content
Mathematics LibreTexts

1.6.9.2.4: Good, Bad, and Not-too-bad

  • Page ID
    32671
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    This last section is even more practical. Let us discuss several R scripts.

    Good

    This is an example of (almost) ideal R script:

    Code \(\PageIndex{1}\) (R):

    ### PREPARATIONS
    library(effsize)
    Normal <- function(x) { ifelse(shapiro.test(x)$p.value > 0.05, "NORMAL", "NON-NORMAL")}
    cc <-read.table("http://ashipunov.info/shipunov/open/ceratophyllum.txt",h=TRUE)
    ### DATA PROCESING
    ## check data:
    str(cc)
    head(cc)
    sapply(cc[, 4:5], Normal) # both non-normal
    ## plot it first:
    pdf("plot1.pdf")
    boxplot(cc[, 4:5], ylab="Position of stem, mm on grid")
    dev.off()
    ## we only need effect size:
    cliff.delta(cc$PLANT1, cc$PLANT2)

    Its main features:

    • clearly separated parts: loading of external material (lines 1–12) and processing of the data itself (lines 13–26)
    • package(s) first (line 3), then custom functions (line 5–7), then data (line 9)
    • data is checked (lines 16–18) with str() and then checked for normality
    • after checks, data was plotted first (lines 21–23), then analyzed (line 26)
    • acceptable style
    • every step is commented

    To see how it works, change working directory to where script is located, then load this script into R with:

    Code \(\PageIndex{2}\) (R):

    source("good.r", echo=TRUE)

    Another variant is non-interactive and therefore faster and cleaner. Use Rresults script (works on macOS and Linux) like:

    Code \(\PageIndex{3}\) (R):

    Rresults good.r

    Script will print both input and output to the terminal, plus also save it as a text file and save plots in one PDF file with script name. Since this book is the R script, you will find examples of Rresults output in the on-line book directory\(^{[1]}\).

    Bad

    Now consider the following script:

    Code \(\PageIndex{4}\) (R):

    wiltingdata<-read.table("http://ashipunov.info/shipunov/open/wilting.txt", h=TRUE)
    url.show("http://ashipunov.info/shipunov/open/wilting_c.txt")
    sapply(wiltingdata, Normality)
    willowsdata<-wiltingdata[grep("Salix",wiltingdata$SPECIES),]
    Rro.test(willows[,1],willows[,2])
    summary(K(willows[,1],willows[,2]))
    source("http://ashipunov.info/r/asmisc.r")
    plot(wiltingadta)

    It is really bad, it simply does not work. Problems start on the first line, and both interactive (with source()) and non-interactive (with Rresults) ways will show it like:

    Code \(\PageIndex{5}\) (R):

    wiltingdata<- read.table("http://ashipunov.info/shipunov/open/wilting.txt",h=TRUE)

    Something is really wrong and you will need to find and correct (debug) it. And since code was not commented, you have to guess what author(s) actually wanted.

    Other negative features:

    • no parts, no proper order of loading, checking and plotting
    • interactive url.show() will block non-interactive applications and therefore is potentially harmful (not a mistake though)
    • bad style: in particular, no spaces around assignments and no spaces after commas
    • very long object names (hard to type)

    Debugging process will consist of multiple tries until we make the working (preferably in the sensible way), “not-too-bad” script. This could be prettified later, most important is to make it work.

    There are many ways to debug. For example, you can open (1) R in the terminal, (2) text editor\(^{[2]}\) with your script and probably also some advanced (3) file manager. Run the script first to see the problem. Then copy-paste from R to editor and back again.

    Let us go to the first line problem first. Message is cryptic, but likely this is some conflict between read.table() and the actual data. Therefore, you need to look on data and if you do, you will find that data contains both spaces and tabs. This is why R was confused. You should tell it to use tabs:

    Code \(\PageIndex{6}\) (R):

    read.table("http://ashipunov.info/shipunov/open/wilting.txt", h=TRUE, sep="	")

    First line starts to work. This way, step by step, you will come to the next stage.

    Not too bad

    This is result of debugging. It is not yet fully prettified, there are no chapters and comments. However, it works and likely in the way implied by authors.

    Code \(\PageIndex{7}\) (R):

    wilt <- read.table("http://ashipunov.info/shipunov/open/wilting.txt",h=TRUE, sep="	")
    ## url.show("http://ashipunov.info/shipunov/open/wilting_c.txt")
    source("http://ashipunov.info/r/asmisc.r")
    str(wilt)
    Normality(wilt[, 2])
    willowsdata <- wilt[grep("Salix", wilt$SPECIES), ]
    willowsdata$SPECIES <- droplevels(willowsdata$SPECIES)
    willows <- split(willowsdata$TIME, willowsdata$SPECIES)
    pdf("willows.pdf")
    boxplot(willows, ylab="Wilting time, minutes")
    dev.off()
    Rro.test(willows[[1]], willows[[2]])
    summary(K(willows[[1]], willows[[2]]))

    What was changed

    • custom commands moved up to line 3 (not to the proper place, better would be line 1, but this position garantrees work)
    • url.show() commented out
    • checks added (lines 5–6)
    • names shortened a bit and style improved (not very important but useful)
    • plotting now plots to file, not just to screen device
    • object willows appeared out of nowhere, therefore we had to guess what is it, why it was used, and then somehow recreate it (lines 8–9)

    object but it is not the same as in initial script. What is different? Is it possible to make them the same?

    References

    1. There is, by the way, a life-hack for lazy reader: all plots which you need to make yourself are actually present in the output PDF file.

    2. Among text editors, Geany is one of the most universal, fast, free and works on most operation sys- tems.


    This page titled 1.6.9.2.4: Good, Bad, and Not-too-bad is shared under a Public Domain license and was authored, remixed, and/or curated by Alexey Shipunov.

    • Was this article helpful?