<h3>Improve references to computer code, software, packages, functions, etc.</h3>
<p>Questions and answers on CV.SE will often refer to computer code, software, packages, or individual functions.  That might include full chunks of code for implementation of statistical methods discussed in the post, or it might include in-text references to packages and functions that are useful for the methods.  Here are some tips for &quot;best practice&quot; with regard to improving these aspects of a post:</p>
<ul>
<li><p><strong>Use <code>computer-code font</code> for all references to code, software and packages:</strong> Full code chunks are automatically shown using <code>computer-code font</code> but it is also desirable for in-text references to these things to appear in this font.  (You should also make sure you use correct capitalisation of names of packages and functions in any software applications that are case-sensitive.)  This assists the reader in seeing which aspects of the discussion pertain to software implementation, and it also ensures that references to packages and functions appear in a consistent font both in the body of the text and in any accompanying code chunks.</p>
</li>
<li><p><strong>State what language you are using:</strong> This may seem an obvious point, but sometimes posters forget to specify the language they are using for their code (e.g., <code>Python</code>, <code>R</code>, <code>Stata</code>, etc.).  Many experienced coders will recognise the language, but not all users will.  Best practice here is to <em>explicitly</em> state the coding language you are using.</p>
</li>
<li><p><strong>Add relevant external links for packages and functions:</strong> When giving an in-text reference to a package for a piece of statistical software, it is desirable to give an external link to the relevant documentation for the package.  When giving an in-text reference to a function, it is desirable to give an external link to the page showing the information for that function (e.g., function arguments, output, description, examples, etc.).  You only need to do this once, so that the reader has a link to relevant documentation, so don't feel the need to link over and over again if you mention a package or function multiple times.  Links may not be necessary for functions and packages that are in common use, but they are extremely helpful for obscure functions or packages.  For packages in <code>R</code> you can link to their page in <a href="https://cran.r-project.org/" rel="nofollow noreferrer">CRAN</a> and for functions in <code>R</code> you can link to their information pages at <a href="https://rdrr.io/" rel="nofollow noreferrer">rdrr.io</a> or <a href="http://finzi.psych.upenn.edu/" rel="nofollow noreferrer">the UPenn R help pages</a>.  These sources give useful links for readers to help them understand the syntax used for a package or function.</p>
</li>
<li><p><strong>Make sure your code runs and is replicable:</strong> When you give chunks of computer code you should check to make sure that your code runs on a newly loaded console, without any other preliminary commands that have not been included.  You may assume that the user has the relevant software and has downloaded all required packages, but you need to remember to include commands to load relevant packages if you are using functions from them.  If you are doing randomisation then you should also &quot;set the seed&quot; to ensure that any pseudo-random numbers generated in the code are replicable.  If you include graphical output in your post then you should make sure that the graphical output you give is what is produced in your code.  (There may be some cases where you give a graph without showing the code, in which case this does not apply.)</p>
</li>
<li><p><strong>Use proper annotation in chunks of computer code:</strong> Even though questions and answers will usually have some accompanying text giving context to any relevant computer code, it is still good practice to ensure that chunks of code are annotated with labels that break up the parts into manageable chunks, and tell the reader what the parts of the code are doing.  As a general rule, the code should not be out of place in an application where the user does not have access to the original question/answer.</p>
</li>
<li><p><strong>Avoiding hard-coding specific parameter values in code:</strong> Many questions on CV.SE ask for the answer to a specific statistical question with a particular set of parameters.  Notwithstanding this fact, the goal of answers is to be broadly applicable to variations of the question that might be of interest to other readers.  This means that when giving computer code for answers, it is often useful to separate out the specific parameter values used for the particular question from the general form of the code to get the answer.  Rather than &quot;hard-coding&quot; the parameter values, instead add a line of code defining each relevant parameter value and then reference these values in the code.  This makes it easier for readers to substitute other parameter values if they want to apply your method to their own problem.</p>
</li>
<li><p><strong>Use helpful names for things in code:</strong> Ideally, all variables and other objects in your code should have names that bear a close resemblance to their actual content, or to the notation usually used to represent them in statistical discussions.  For small pieces of code it is usually possible to name your objects in a helpful way, but this may become cumbersome if you have a long piece of code with many intermediate objects, so some reasonable judgment is needed.  In any case, consider naming your data as <code>DATA</code> and your model as <code>MODEL</code> and so on, to make it obvious to the reader what these objects are.  In programs like <code>R</code> that are case-sensitive, one useful thing you can do is to name your objects with upper-case and then have these roughly match up with argument inputs of functions which are written in lower-case (e.g., if you have a function that takes an input <code>slope</code> then create a variable <code>SLOPE</code> for its value and then input it to the function as <code>slope = SLOPE</code>).</p>
</li>
</ul>
<p>In the section below I show an example of a bad version of a question that uses poor practices for coding, and then an edited version that applies the above practices.</p>
<hr />
<p><strong>Bad version:</strong></p>
<p>You can implement Kaplan-Meier using bootstrap methods with the bootkm function in hmisc, but you will need to set <span class="math-container">$q = 0.8$</span> because that is not the default.  Here is how you do this with one-hundred bootstrap repetitions for a test of quantile difference for a variable for males and females:</p>
<pre><code>S &lt;- Surv(runif(400))
dd &lt;- data.frame(var = S, type = c(rep('M', 200), rep('F', 200)))
ssm &lt;- bootkm(a['type' == 'M'], b = 100, q = 0.8)
ssf &lt;- bootkm(a['type' == 'F'], b = 100, q = 0.8)
describe(ssm-ssf)
quantile(ssm-ssf)
</code></pre>
<p><strong>Good version:</strong></p>
  
<p>You can implement Kaplan-Meier using bootstrap methods with the <a href="http://finzi.psych.upenn.edu/R/library/Hmisc/html/bootkm.html" rel="nofollow noreferrer"><code>bootkm</code></a> function in the <a href="http://math.furman.edu/%7Edcs/courses/math47/R/library/Hmisc/html/Overview.html" rel="nofollow noreferrer"><code>Hmisc</code> package</a> in <code>R</code>.  The function takes an input <code>q</code> for the quantile, so you will need to set this to your desired value.  The function also takes an input <code>b</code> for the number of bootstrap iterations (500 by default), but we will only use 100 iterations.  Here is how you perform 100 bootstrap iterations for a test of quantile difference for the survival time for males and females in a mock set of data (where the true difference is zero):</p>
<pre><code># Load required libraries
library(survival)
library(Hmisc)

# Create some mock survival data (using the survival package)
# Survival times for a sample of males (M) and females (F)
set.seed(1)
n &lt;- 400
TIME &lt;- Surv(runif(n))
DATA &lt;- data.frame(Time = TIME, Sex = c(rep('M', 200), 
                    rep('F', 200)))

# Perform the bootstrap iterations (using the Hmisc package)
QUANTILE    &lt;- 0.8
ITERATIONS  &lt;- 100
BOOT.MALE   &lt;- bootkm(DATA[DATA<span class="math-container">$Sex == 'M', ], b = ITERATIONS, 
                  q = QUANTILE)
BOOT.FEMALE &lt;- bootkm(DATA[DATA$</span>Sex == 'F', ], b = ITERATIONS, 
                      q = QUANTILE)

# Describe the quantile difference
describe(BOOT.MALE-BOOT.FEMALE)
quantile(BOOT.MALE-BOOT.FEMALE)
</code></pre>
