1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
# Statistics Menu dialogs
# last modified 2022-06-30 by J. Fox
# Nonparametric tests menu
twoSampleWilcoxonTest <- function () {
defaults <- list(initial.group = NULL, initial.response = NULL, initial.alternative = "two.sided",
initial.test = "default", initial.label=NULL, initial.tab=0)
dialog.values <- getDialog("twoSampleWilcoxonTest", defaults)
initializeDialog(title = gettextRcmdr("Two-Sample Wilcoxon Test"), use.tabs=TRUE)
groupBox <- variableListBox(dataTab, TwoLevelFactors(), title = gettextRcmdr("Groups (pick one)"),
initialSelection = varPosn(dialog.values$initial.group, "twoLevelFactor"))
responseBox <- variableListBox(dataTab, Numeric(), title = gettextRcmdr("Response Variable (pick one)"),
initialSelection = varPosn(dialog.values$initial.response, "numeric"))
onOK <- function() {
tab <- if (as.character(tkselect(notebook)) == dataTab$ID) 0 else 1
group <- getSelection(groupBox)
if (length(group) == 0) {
errorCondition(recall = twoSampleWilcoxonTest, message = gettextRcmdr("You must select a groups variable."))
return()
}
response <- getSelection(responseBox)
if (length(response) == 0) {
errorCondition(recall = twoSampleWilcoxonTest, message = gettextRcmdr("You must select a response variable."))
return()
}
alternative <- as.character(tclvalue(alternativeVariable))
test <- as.character(tclvalue(testVariable))
closeDialog()
putDialog("twoSampleWilcoxonTest", list(initial.group = group, initial.response = response,
initial.test = test, initial.alternative = alternative, initial.label=.groupsLabel, initial.tab=tab))
.activeDataSet <- ActiveDataSet()
# doItAndPrint(paste("with(", .activeDataSet, ", tapply(", paste(
# response, sep = ""), ", ", paste(
# group, sep = ""), ", median, na.rm=TRUE))", sep = ""))
doItAndPrint(paste0("Tapply(", response, " ~ ", group, ", median, na.action=na.omit, data=",
.activeDataSet, ") # medians by group"))
if (test == "default") {
doItAndPrint(paste("wilcox.test(", response, " ~ ",
group, ", alternative=\"", alternative, "\", data=",
.activeDataSet, ")", sep = ""))
}
else doItAndPrint(paste("wilcox.test(", response, " ~ ",
group, ", alternative='", alternative, "', exact=",
test == "exact", ", correct=", test == "correct",
", data=", .activeDataSet, ")", sep = ""))
tkfocus(CommanderWindow())
}
OKCancelHelp(helpSubject = "wilcox.test", reset = "twoSampleWilcoxonTest",
apply = "twoSampleWilcoxonTest")
radioButtons(optionsTab, name = "alternative", buttons = c("twosided",
"less", "greater"), values = c("two.sided", "less", "greater"),
labels = gettextRcmdr(c("Two-sided", "Difference < 0",
"Difference > 0")), initialValue = dialog.values$initial.alternative,
title = gettextRcmdr("Alternative Hypothesis"))
radioButtons(optionsTab, name = "test", buttons = c("default", "exact",
"normal", "correct"), labels = gettextRcmdr(c("Default",
"Exact", "Normal approximation", "Normal approximation with\ncontinuity correction")),
initialValue = dialog.values$initial.test,
title = gettextRcmdr("Type of Test"))
tkgrid(getFrame(groupBox), labelRcmdr(dataTab, text=" "), getFrame(responseBox), sticky = "nw")
groupsLabel(optionsTab, groupsBox = groupBox, columnspan = 3, initialText=dialog.values$initial.label)
tkgrid(alternativeFrame, labelRcmdr(optionsTab, text=" "), testFrame, sticky = "nw")
dialogSuffix(use.tabs=TRUE, grid.buttons=TRUE)
}
pairedWilcoxonTest <- function () {
defaults <- list(initial.x = NULL, initial.y = NULL, initial.alternative = "two.sided",
initial.test = "default", initial.tab=0)
dialog.values <- getDialog("pairedWilcoxonTest", defaults)
initializeDialog(title = gettextRcmdr("Paired Wilcoxon Test"), use.tabs=TRUE)
.numeric <- Numeric()
xBox <- variableListBox(dataTab, .numeric, title = gettextRcmdr("First variable (pick one)"),
initialSelection = varPosn(dialog.values$initial.x, "numeric"))
yBox <- variableListBox(dataTab, .numeric, title = gettextRcmdr("Second variable (pick one)"),
initialSelection = varPosn(dialog.values$initial.y, "numeric"))
onOK <- function() {
tab <- if (as.character(tkselect(notebook)) == dataTab$ID) 0 else 1
x <- getSelection(xBox)
y <- getSelection(yBox)
closeDialog()
alternative <- as.character(tclvalue(alternativeVariable))
test <- as.character(tclvalue(testVariable))
putDialog("pairedWilcoxonTest", list(initial.x = x, initial.y = y,
initial.test = test, initial.alternative = alternative, initial.tab=tab))
if (length(x) == 0 | length(y) == 0) {
errorCondition(recall = pairedWilcoxonTest, message = gettextRcmdr("You must select two variables."))
return()
}
if (x == y) {
errorCondition(recall = pairedWilcoxonTest, message = gettextRcmdr("The two variables must be different."))
return()
}
.activeDataSet <- ActiveDataSet()
doItAndPrint(paste("with(", .activeDataSet, ", median(", x,
" - ", y, ", na.rm=TRUE)) # median difference",
sep = ""))
if (test == "default") {
doItAndPrint(paste("with(", .activeDataSet, ", wilcox.test(",
x, ", ", y, ", alternative='",
alternative, "', paired=TRUE))", sep = ""))
}
else if (test == "exact") {
doItAndPrint(paste("with(", .activeDataSet, ", wilcox.test(",
x, ", ", y, ", alternative='",
alternative, "', exact=TRUE, paired=TRUE))", sep = ""))
}
else {
doItAndPrint(paste("with(", .activeDataSet, ", wilcox.test(",
x, ", ",y, ", alternative='",
alternative, "', correct=", test == "correct",
", exact=FALSE, paired=TRUE))", sep = ""))
}
insertRmdSection(paste0(gettextRmdHeader("Paired Wilcoxon Test: "), x, ", ", y))
tkfocus(CommanderWindow())
}
OKCancelHelp(helpSubject = "wilcox.test", reset = "pairedWilcoxonTest",
apply = "pairedWilcoxonTest")
radioButtons(optionsTab, name = "alternative", buttons = c("twosided",
"less", "greater"), values = c("two.sided", "less", "greater"),
labels = gettextRcmdr(c("Two-sided", "Difference < 0",
"Difference > 0")), title = gettextRcmdr("Alternative Hypothesis"),
initialValue = dialog.values$initial.alternative)
radioButtons(optionsTab, name = "test", buttons = c("default", "exact",
"normal", "correct"), labels = gettextRcmdr(c("Default",
"Exact", "Normal approximation", "Normal approximation with\ncontinuity correction")),
title = gettextRcmdr("Type of Test"), initialValue = dialog.values$initial.test)
tkgrid(getFrame(xBox), labelRcmdr(dataTab, text=" "), getFrame(yBox), sticky = "nw")
tkgrid(alternativeFrame, labelRcmdr(optionsTab, text=" "), testFrame, sticky = "nw")
dialogSuffix(use.tabs=TRUE, grid.buttons=TRUE)
}
KruskalWallisTest <- function () {
defaults <- list(initial.group = NULL, initial.response = NULL)
dialog.values <- getDialog("KruskalWallisTest", defaults)
initializeDialog(title = gettextRcmdr("Kruskal-Wallis Rank Sum Test"))
dataFrame <- tkframe(top)
groupBox <- variableListBox(dataFrame, Factors(), title = gettextRcmdr("Groups (pick one)"),
initialSelection = varPosn(dialog.values$initial.group, "factor"))
responseBox <- variableListBox(dataFrame, Numeric(), title = gettextRcmdr("Response Variable (pick one)"),
initialSelection = varPosn(dialog.values$initial.response, "numeric"))
onOK <- function() {
group <- getSelection(groupBox)
if (length(group) == 0) {
errorCondition(recall = KruskalWallisTest, message = gettextRcmdr("You must select a groups variable."))
return()
}
response <- getSelection(responseBox)
closeDialog()
putDialog("KruskalWallisTest", list(initial.group = group, initial.response = response))
if (length(response) == 0) {
errorCondition(recall = KruskalWallisTest, message = gettextRcmdr("You must select a response variable."))
return()
}
.activeDataSet <- ActiveDataSet()
# doItAndPrint(paste("with(", .activeDataSet, ", tapply(", paste(
# response, sep = ""), ", ", paste(
# group, sep = ""), ", median, na.rm=TRUE))", sep = ""))
doItAndPrint(paste0("Tapply(", response, " ~ ", group, ", median, na.action=na.omit, data=",
.activeDataSet, ") # medians by group"))
doItAndPrint(paste("kruskal.test(", response, " ~ ",
group, ", data=", .activeDataSet, ")", sep = ""))
tkfocus(CommanderWindow())
}
OKCancelHelp(helpSubject = "kruskal.test", reset = "KruskalWallisTest",
apply = "KruskalWallisTest")
tkgrid(getFrame(groupBox), labelRcmdr(dataFrame, text=" "), getFrame(responseBox), sticky = "nw")
tkgrid(dataFrame, sticky="w")
tkgrid(buttonsFrame, sticky = "ew")
dialogSuffix()
}
FriedmanTest <- function () {
defaults <- list(initial.response = NULL)
dialog.values <- getDialog("FriedmanTest", defaults)
initializeDialog(title = gettextRcmdr("Friedman Rank Sum Test"))
responseBox <- variableListBox(top, Numeric(), selectmode = "multiple",
initialSelection = varPosn(dialog.values$initial.response, "numeric"),
title = gettextRcmdr("Repeated-Measures Variables (pick two or more)"))
onOK <- function() {
responses <- getSelection(responseBox)
closeDialog()
putDialog("FriedmanTest", list (initial.response = responses))
if (length(responses) < 2) {
errorCondition(recall = FriedmanTest, message = gettextRcmdr("You must select at least two variables."))
return()
}
.activeDataSet <- ActiveDataSet()
command <- paste("local({\n .Responses <- na.omit(with(", .activeDataSet, ", cbind(",
paste(responses, collapse = ", "), ")))", sep = "")
command <- paste(command, '\n cat("\\nMedians:\\n")', sep="")
command <- paste(command, "\n print(apply(.Responses, 2, median))")
command <- paste(command, "\n friedman.test(.Responses)\n})")
doItAndPrint(command)
insertRmdSection(paste0(gettextRmdHeader("Friedman Rank Sum Test: "),
paste(responses, collapse=", ")))
tkfocus(CommanderWindow())
}
OKCancelHelp(helpSubject = "friedman.test", reset = "FriedmanTest",
apply = "FriedmanTest")
tkgrid(getFrame(responseBox), sticky = "nw")
tkgrid(buttonsFrame, sticky = "w")
dialogSuffix()
}
onesampleWilcoxonTest <- function () {
defaults <- list(initial.x = NULL, initial.alternative = "two.sided",
initial.test = "default", initial.mu = "0.0", initial.tab=0)
dialog.values <- getDialog("onesampleWilcoxonTest", defaults)
initializeDialog(title = gettextRcmdr("Single-Sample Wilcoxon Test"), use.tabs=TRUE)
.numeric <- Numeric()
xBox <- variableListBox(dataTab, .numeric, title = gettextRcmdr("Variable (pick one)"),
initialSelection = varPosn(dialog.values$initial.x, "numeric"))
onOK <- function() {
tab <- if (as.character(tkselect(notebook)) == dataTab$ID) 0 else 1
x <- getSelection(xBox)
mu <- tclvalue(muVariable)
closeDialog()
alternative <- as.character(tclvalue(alternativeVariable))
test <- as.character(tclvalue(testVariable))
putDialog("onesampleWilcoxonTest", list(initial.x = x,
initial.test = test, initial.alternative = alternative, initial.mu = mu, initial.tab=tab))
if (length(x) == 0) {
errorCondition(recall = onesampleWilcoxonTest, message = gettextRcmdr("You must select a variable."))
return()
}
.activeDataSet <- ActiveDataSet()
null <- paste("', mu=", mu, sep="")
doItAndPrint(paste("with(", .activeDataSet, ", median(", x, ", na.rm=TRUE))",
sep = ""))
doItAndPrint(paste("with(", .activeDataSet, ", mean(", x, ", na.rm=TRUE))",
sep = ""))
if (test == "default") {
doItAndPrint(paste("with(", .activeDataSet, ", wilcox.test(",
x, ", alternative='",
alternative, null, "))", sep = ""))
}
else if (test == "exact") {
doItAndPrint(paste("with(", .activeDataSet, ", wilcox.test(",
x, ", alternative='",
alternative, null, ", exact=TRUE))", sep = ""))
}
else {
doItAndPrint(paste("with(", .activeDataSet, ", wilcox.test(",
x, ", alternative='",
alternative, null, ", correct=", test == "correct",
", exact=FALSE))", sep = ""))
}
insertRmdSection(paste0(gettextRmdHeader("Single-Sample Wilcoxon Test: "), x))
tkfocus(CommanderWindow())
}
OKCancelHelp(helpSubject = "wilcox.test", reset = "onesampleWilcoxonTest",
apply = "onesampleWilcoxonTest")
radioButtons(optionsTab, name = "alternative", buttons = c("twosided",
"less", "greater"), values = c("two.sided", "less", "greater"),
labels = gettextRcmdr(c("Two-sided", "mu < 0",
"mu > 0")), title = gettextRcmdr("Alternative Hypothesis"),
initialValue = dialog.values$initial.alternative)
radioButtons(optionsTab, name = "test", buttons = c("default", "exact",
"normal", "correct"), labels = gettextRcmdr(c("Default",
"Exact", "Normal approximation", "Normal approximation with\ncontinuity correction")),
title = gettextRcmdr("Type of Test"), initialValue = dialog.values$initial.test)
muFrame <- tkframe(optionsTab)
muVariable <- tclVar(dialog.values$initial.mu)
muField <- ttkentry(muFrame, width = "8", textvariable = muVariable)
tkgrid(labelRcmdr(muFrame, text = gettextRcmdr("Null hypothesis: mu =")),
muField, sticky = "w")
tkgrid(muFrame, sticky = "w")
tkgrid(getFrame(xBox), labelRcmdr(dataTab, text=" "), sticky = "nw")
tkgrid(alternativeFrame, labelRcmdr(optionsTab, text=" "), testFrame, sticky = "nw")
dialogSuffix(use.tabs=TRUE, grid.buttons=TRUE)
}
|