Commit e4d85443 authored by Thomas Friedrichsmeier's avatar Thomas Friedrichsmeier
Browse files

Add i18n() to "Descriptive Statistics" plugin

parent d9b79205
......@@ -3,79 +3,69 @@ var mad_type;
var constMad;
function calculate () {
var vars = trim (getValue ("x")).split ("\n").join (", ");
var vars = getList ("x");
constMad = getValue ("constMad");
mad_type = getValue ("mad_type");
echo ('vars <- rk.list (' + vars + ')\n');
echo ('results <- data.frame (\'Object\'=I(names (vars)))\n');
echo ('vars <- rk.list (' + vars.join (", ") + ')\n');
echo ('results <- data.frame (' + i18n ("Object") + '=I(names (vars)))\n');
echo ('for (i in 1:length (vars)) {\n');
echo (' var <- vars[[i]]\n');
echo ('\n');
echo (' # we wrap each single call in a "try" statement to always continue on errors.\n');
comment ('we wrap each single call in a "try" statement to always continue on errors.', '\t');
if (getValue ("mean")) {
// trim is the fraction (0 to 0.5) of observations to be trimmed from each end of x before the mean is computed
echo (' results[i, \'mean\'] <- try (mean (var, trim = ' + getValue ("trim") + ', na.rm=TRUE))\n');
echo (' results[i, ' + i18n ("mean") + '] <- try (mean (var, trim = ' + getValue ("trim") + ', na.rm=TRUE))\n');
}
if (getValue ("geo_mean")) {
// compute the geometric mean
echo (' results[i, \'geometric mean\'] <- try (prod (na.omit(var))^(1 / length (na.omit(var))))\n');
echo (' results[i, ' + i18n ("geometric mean") + '] <- try (prod (na.omit(var))^(1 / length (na.omit(var))))\n');
}
if (getValue ("interquantile_mean")) {
// compute the quartile (25% and 75%) mean
echo (' results[i, \'interquantile mean\'] <- try (sum(quantile(var, probs=c(0.25), na.rm=T), quantile(var, probs=c(0.75), na.rm=TRUE)) / 2)\n');
echo (' results[i, ' + i18n ("interquantile mean") + '] <- try (sum(quantile(var, probs=c(0.25), na.rm=T), quantile(var, probs=c(0.75), na.rm=TRUE)) / 2)\n');
}
if (getValue ("harmonic_mean")) {
// compute the harmonic mean
echo (' results[i, \'harmonic mean\'] <- try (1 / mean(1 / na.omit(var)))\n');
echo (' results[i, ' + i18n ("harmonic mean") + '] <- try (1 / mean(1 / na.omit(var)))\n');
}
if (getValue ("median")) {
echo (' results[i, \'median\'] <- try (median (var, na.rm=TRUE))\n');
echo (' results[i, ' + i18n ("median") + '] <- try (median (var, na.rm=TRUE))\n');
}
if (getValue ("range")) {
echo (' try ({\n');
echo (' range <- try (range (var, na.rm=TRUE))\n');
echo (' results[i, \'min\'] <- range[1]\n');
echo (' results[i, \'max\'] <- range[2]\n');
echo (' results[i, ' + i18n ("min") + '] <- range[1]\n');
echo (' results[i, ' + i18n ("max") + '] <- range[2]\n');
echo (' })\n');
}
if (getValue ("sd")) {
echo (' results[i, \'standard deviation\'] <- try (sd (var, na.rm=TRUE))\n');
echo (' results[i, ' + i18n ("standard deviation") + '] <- try (sd (var, na.rm=TRUE))\n');
}
if (getValue ("sum")) {
echo (' results[i, \'sum\'] <- try (sum (var, na.rm=TRUE))\n');
echo (' results[i, ' + i18nc ("noun", "sum") + '] <- try (sum (var, na.rm=TRUE))\n');
}
if (getValue ("prod")) {
echo (' results[i, \'product\'] <- try (prod (var, na.rm=TRUE))\n');
echo (' results[i, ' + i18n ("product") + '] <- try (prod (var, na.rm=TRUE))\n');
}
if (getValue ("mad")) {
echo (' results[i, \'Median Absolute Deviation\'] <- try (mad (var, constant = ' + constMad);
echo (' results[i, ' + i18n ("Median Absolute Deviation") + '] <- try (mad (var, constant = ' + constMad);
if (mad_type == "low") echo (", low=TRUE");
else if (mad_type == "high") echo (", high=TRUE");
echo (', na.rm=TRUE))\n');
}
if (getValue ("length")) {
echo (' results[i, \'length of sample\'] <- length (var)\n');
echo (' results[i, \'number of NAs\'] <- sum (is.na(var))\n');
echo (' results[i, ' + i18n ("length of sample") + '] <- length (var)\n');
echo (' results[i, ' + i18n ("number of NAs") + '] <- sum (is.na(var))\n');
}
echo ('}\n');
}
function printout () {
echo ('rk.header ("Descriptive statistics", parameters=list (\n');
echo (' "Trim of mean", ' + getValue ("trim"));
new Header (i18n ("Descriptive statistics")).addFromUI ("trim").print ();
if (getValue ("mad")) {
echo (',\n');
echo (' "Median Absolute Deviation",\n');
echo (' paste ("constant:", ' + constMad + ', ');
if (mad_type == "low") echo ('"lo-median"');
else if (mad_type == "high") echo ('"hi-median"');
else echo ('"average"' + ')');
new Header (i18n ("Median Absolute Deviation"), 3).addFromUI ("constMad").addFromUI ("mad_type").print ();
}
echo ('))\n');
echo ('\n');
echo ('rk.results (results)\n');
if (getValue ("save_to_file")) echo ('write.csv(file="' + getValue ("file") + '", results)\n');
}
local({
## Compute
vars <- rk.list (women[["height"]], test10z)
results <- data.frame ('Object'=I(names (vars)))
results <- data.frame ("Object"=I(names (vars)))
for (i in 1:length (vars)) {
var <- vars[[i]]
# we wrap each single call in a "try" statement to always continue on errors.
results[i, 'mean'] <- try (mean (var, trim = 0.00, na.rm=TRUE))
results[i, 'median'] <- try (median (var, na.rm=TRUE))
results[i, "mean"] <- try (mean (var, trim = 0.00, na.rm=TRUE))
results[i, "median"] <- try (median (var, na.rm=TRUE))
try ({
range <- try (range (var, na.rm=TRUE))
results[i, 'min'] <- range[1]
results[i, 'max'] <- range[2]
results[i, "min"] <- range[1]
results[i, "max"] <- range[2]
})
results[i, 'standard deviation'] <- try (sd (var, na.rm=TRUE))
results[i, 'sum'] <- try (sum (var, na.rm=TRUE))
results[i, 'product'] <- try (prod (var, na.rm=TRUE))
results[i, 'Median Absolute Deviation'] <- try (mad (var, constant = 1.4628, na.rm=TRUE))
results[i, 'length of sample'] <- length (var)
results[i, 'number of NAs'] <- sum (is.na(var))
results[i, "standard deviation"] <- try (sd (var, na.rm=TRUE))
results[i, "sum"] <- try (sum (var, na.rm=TRUE))
results[i, "product"] <- try (prod (var, na.rm=TRUE))
results[i, "Median Absolute Deviation"] <- try (mad (var, constant = 1.4628, na.rm=TRUE))
results[i, "length of sample"] <- length (var)
results[i, "number of NAs"] <- sum (is.na(var))
}
## Print result
rk.header ("Descriptive statistics", parameters=list (
"Trim of mean", 0.00,
"Median Absolute Deviation",
paste ("constant:", 1.4628, "average")))
rk.header ("Descriptive statistics", parameters=list("Trim of mean"="0.00"))
rk.header ("Median Absolute Deviation", parameters=list("Constant"="1.4628",
"Treatment of even sample sizes"="use average"), level=3)
rk.results (results)
})
<h1>Descriptive statistics</h1>
<h2>Parameters</h2>
<ul><li>Trim of mean: 0</li>
<li>Median Absolute Deviation: constant: 1.4628 average</li>
<ul><li>Trim of mean: 0.00</li>
</ul>
DATE<br />
<h3>Median Absolute Deviation</h3>
<h4>Parameters</h4>
<ul><li>Constant: 1.4628</li>
<li>Treatment of even sample sizes: use average</li>
</ul>
<br />
<table border="1">
<tr><td>Object</td><td>mean</td><td>median</td><td>min</td><td>max</td><td>standard deviation</td><td>sum</td><td>product</td><td>Median Absolute Deviation</td><td>length of sample</td><td>number of NAs</td></tr>
<tr><td>height</td><td>65</td><td>65</td><td>58</td><td>72</td><td>4.4721</td><td>975</td><td>1.511e+27</td><td>5.8512</td><td>15</td><td>0</td></tr>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment