Skip to content

Commit

Permalink
replace SignedDiff with PairedDifference
Browse files Browse the repository at this point in the history
  • Loading branch information
qddyy committed Jan 6, 2024
1 parent d1da1b5 commit 61b66f4
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 113 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export(MultiCompT)
export(MultipleComparison)
export(OneSampleTest)
export(Page)
export(PairedDifference)
export(PermuTest)
export(Quantile)
export(RCBD)
Expand All @@ -24,7 +25,6 @@ export(RatioMeanDeviance)
export(ScoreSum)
export(SiegelTukey)
export(Sign)
export(SignedDiff)
export(TukeyHSD)
export(TwoSampleAssociationTest)
export(TwoSamplePairedTest)
Expand Down
40 changes: 19 additions & 21 deletions R/SignedDiff.R → R/PairedDifference.R
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
#' @title `r SignedDiff$private_fields$.name`
#' @title `r PairedDifference$private_fields$.name`
#'
#' @description Performs two sample signed score test on data vectors.
#'
#' @aliases paired.signeddiff
#' @aliases paired.difference
#'
#' @export
#'
#' @importFrom R6 R6Class
#' @importFrom stats pnorm


SignedDiff <- R6Class(
classname = "SignedDiff",
PairedDifference <- R6Class(
classname = "PairedDifference",
inherit = TwoSamplePairedTest,
cloneable = FALSE,
public = list(
#' @description Create a new `SignedDiff` object.
#' @description Create a new `PairedDifference` object.
#'
#' @template init_params
#' @param scoring a character string specifying which scoring system to be used on the absolute differences.
#' @param method a character string specifying the method of ranking data in computing adjusted signed ranks for tied data, must be one of `"with_zeros"` (default) or `"ignore"`. Note that the data will be modified when this parameter is set to `"ignore"`.
#' @param correct a logical indicating whether to apply continuity correction in the normal approximation for the p-value when `scoring` is set to `"rank"`.
#'
#' @return A `SignedDiff` object.
#' @return A `PairedDifference` object.
initialize = function(
type = c("permu", "asymp"), method = c("with_zeros", "ignore"), correct = TRUE,
type = c("permu", "asymp"), method = c("with_zeros", "without_zeros"), correct = TRUE,
alternative = c("two_sided", "less", "greater"), n_permu = 0L, scoring = c("none", "rank", "vw", "expon")
) {
private$.correct <- correct
Expand All @@ -35,7 +35,7 @@ SignedDiff <- R6Class(
}
),
private = list(
.name = "Paired Comparison Based on Signed Differences",
.name = "Paired Comparison Based on Differences",

.correct = NULL,

Expand All @@ -44,27 +44,25 @@ SignedDiff <- R6Class(
.define = function() {
diff <- private$.data$x - private$.data$y

if (private$.method == "ignore") {
private$.data <- private$.data[diff != 0, ]
diff <- diff[diff != 0]
}
where_zero <- (diff == 0)

private$.abs_diff <- abs_diff <- abs(diff)
if (private$.scoring != "none") {
private$.abs_diff <- get_score(
private$.abs_diff, method = private$.scoring
)
if (private$.method == "without_zeros") {
diff <- diff[!where_zero]
private$.data <- private$.data[!where_zero, ]
}

diff_positive <- (diff > 0)
private$.abs_diff <- abs_diff <- if (private$.scoring != "none") {
score <- get_score(abs(diff), method = private$.scoring)
if (private$.method == "with_zeros") `[<-`(score, where_zero, 0) else score
} else abs(diff)

positive <- (diff > 0)
private$.statistic_func <- function(swapped) {
sum(abs_diff[diff_positive != swapped])
sum(abs_diff[positive != swapped])
}
},

.calculate_p = function() {
n <- nrow(private$.data)

z <- private$.statistic - sum(private$.abs_diff) / 2
correction <- if (private$.scoring == "rank" & private$.correct) {
switch(private$.side, lr = sign(z) * 0.5, r = 0.5, l = -0.5)
Expand Down
2 changes: 1 addition & 1 deletion R/pmt.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ tests <- list(
multicomp.tukey = TukeyHSD,

paired.sign = Sign,
paired.signeddiff = SignedDiff,
paired.difference = PairedDifference,

rcbd.anova = RCBDANOVA,
rcbd.friedman = Friedman,
Expand Down
16 changes: 3 additions & 13 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,10 @@ set.seed(0)
t$p_value
```
There is also support for chaining methods, which means that you can do things like
All available tests:
```{r, chaining, eval = FALSE}
t <- pmt(...)$test(...)$print(...)$plot(...)
```{r}
pmts()
```

## Tips

Check

- `?PermuTest` (all tests' base class) for all available methods and attributes.
- `pmts()` for all available tests.
```{r, pmts}
pmts()
```
## References
70 changes: 28 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,53 +82,39 @@ set.seed(0)
#> [1] 0.0005090729
```

There is also support for chaining methods, which means that you can do
things like
All available tests:

``` r
t <- pmt(...)$test(...)$print(...)$plot(...)
pmts()
```

## Tips
<div class="kable-table">

| key | class | test |
|:---------------------|:-------------------|:-----------------------------------------------------|
| onesample.quantile | Quantile | Quantile Test |
| onesample.cdf | CDF | Cumulative Distribution Function |
| twosample.difference | Difference | Two Sample Test Based on Mean or Median |
| twosample.wilcoxon | Wilcoxon | Two Sample Wilcoxon Test |
| twosample.scoresum | ScoreSum | Score Sum Test |
| twosample.ansari | AnsariBradley | Ansari-Bradley Test |
| twosample.siegel | SiegelTukey | Siegel-Tukey Test |
| twosample.rmd | RatioMeanDeviance | Ratio Mean Deviance Test |
| twosample.ks | KolmogorovSmirnov | Two Sample Kolmogorov-Smirnov Test |
| ksample.anova | ANOVA | K Sample Test Based on F Statistic |
| ksample.kw | KruskalWallis | Kruskal-Wallis Test |
| ksample.jt | JonckheereTerpstra | Jonckheere-Terpstra Test |
| multicomp.t | MultiCompT | Multiple Comparison Based on t Statistic |
| multicomp.tukey | TukeyHSD | Tukey’s HSD |
| paired.sign | Sign | Sign Test |
| paired.difference | PairedDifference | Paired Comparison Based on Differences |
| rcbd.anova | RCBDANOVA | ANOVA for Randomized Complete Block Design |
| rcbd.friedman | Friedman | Friedman Test |
| rcbd.page | Page | Page Test |
| association.corr | Correlation | Two Sample Test Based on Correlation Coefficient |
| table.chisq | ChiSquare | Contingency Table Test Based on Chi-square Statistic |

Check

- `?PermuTest` (all tests’ base class) for all available methods and
attributes.

- `pmts()` for all available tests.

``` r
pmts()
```

<div class="kable-table">

| key | class | test |
|:---------------------|:-------------------|:-----------------------------------------------------|
| onesample.quantile | Quantile | Quantile Test |
| onesample.cdf | CDF | Cumulative Distribution Function |
| twosample.difference | Difference | Two Sample Test Based on Mean or Median |
| twosample.wilcoxon | Wilcoxon | Two Sample Wilcoxon Test |
| twosample.scoresum | ScoreSum | Score Sum Test |
| twosample.ansari | AnsariBradley | Ansari-Bradley Test |
| twosample.siegel | SiegelTukey | Siegel-Tukey Test |
| twosample.rmd | RatioMeanDeviance | Ratio Mean Deviance Test |
| twosample.ks | KolmogorovSmirnov | Two Sample Kolmogorov-Smirnov Test |
| ksample.anova | ANOVA | K Sample Test Based on F Statistic |
| ksample.kw | KruskalWallis | Kruskal-Wallis Test |
| ksample.jt | JonckheereTerpstra | Jonckheere-Terpstra Test |
| multicomp.t | MultiCompT | Multiple Comparison Based on t Statistic |
| multicomp.tukey | TukeyHSD | Tukey’s HSD |
| paired.sign | Sign | Sign Test |
| paired.signeddiff | SignedDiff | Paired Comparison Based on Signed Differences |
| rcbd.anova | RCBDANOVA | ANOVA for Randomized Complete Block Design |
| rcbd.friedman | Friedman | Friedman Test |
| rcbd.page | Page | Page Test |
| association.corr | Correlation | Two Sample Test Based on Correlation Coefficient |
| table.chisq | ChiSquare | Contingency Table Test Based on Chi-square Statistic |

</div>
</div>

## References

Expand Down
26 changes: 13 additions & 13 deletions man/SignedDiff.Rd → man/PairedDifference.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 7 additions & 22 deletions vignettes/examples.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,9 @@ t$p_value
## Table 4.1.1

```{r}
# See ?SignedDiff or ?paired.signeddiff
# See ?PairedDifference or ?paired.difference
t <- pmt(
"paired.signeddiff", alternative = "greater",
"paired.difference", alternative = "greater",
scoring = "none"
)
Expand All @@ -356,10 +356,10 @@ t$p_value
## Example 4.1.1

```{r}
# See ?SignedDiff or ?paired.signeddiff
# See ?PairedDifference or ?paired.difference
t <- pmt(
"paired.signeddiff", alternative = "two_sided",
scoring = "none", method = "ignore"
"paired.difference", alternative = "two_sided",
scoring = "none", method = "without_zeros"
)
# See ?Table4.1.3
Expand All @@ -378,9 +378,9 @@ t$p_value
## Example 4.2.1 & 4.2.2

```{r}
# See ?SignedDiff or ?paired.signeddiff
# See ?PairedDifference or ?paired.difference
t <- pmt(
"paired.signeddiff", alternative = "greater",
"paired.difference", alternative = "greater",
scoring = "rank", method = "with_zeros"
)
Expand All @@ -394,21 +394,6 @@ t$type <- "asymp"
t$p_value
```

## Table 4.2.3

```{r}
# See ?SignedDiff or ?paired.signeddiff
t <- pmt(
"paired.signeddiff", alternative = "greater",
scoring = "rank", method = "ignore"
)
t$test(c(-5, -3, -3, 0, 0, 2, 4, 4, 4, 5), 0)
t$statistic
t$p_value
```

## Example 4.3.1

```{r}
Expand Down

0 comments on commit 61b66f4

Please sign in to comment.