Home Comments Thread
New Thread

4 Comments

giscus-bot giscus-bot 2022-12-16 18:20:08
Guest *Tal Galili* @ 2010-04-03 12:56:46 originally posted:

Beautiful - thank you!

Do you believe we will have (in the near future) the ability to do this type of interactivity - but without the picture flickering?

yihui yihui 2022-12-16 18:20:12

Flickering was due to my dragging too fast -- both the graphic widget and the plotting need some time to draw the picture and show it. In real demonstration, the user may not want to drag the slider so fast :)

Originally posted on 2010-04-04 00:22:06

giscus-bot giscus-bot 2022-12-16 18:20:13
Guest *Tal Galili* @ 2010-04-04 01:37:52 originally posted:

Interesting to know - thanks again :)

giscus-bot giscus-bot 2022-12-16 18:20:09
Guest *Max* @ 2010-05-04 22:54:28 originally posted:

This looks cool, but I get all sorts of very interesting errors after running this code... This is after installing all of the packages. I tried to get running just in R, I did install that Gtk+ software (no idea what it is or what it does) any suggestions?

yihui yihui 2022-12-16 18:20:14

I'm so sorry that I did not check the gWidgetstcltk package carefully. The above code will fail if we use tcltk (options(guiToolkit = 'tcltk')), and it's OK with RGtk2 (options(guiToolkit = "RGtk2")).

Note that after you have installed GTK+, you need to restart R. Let me know if you have further problems.

Originally posted on 2010-05-04 23:40:04

giscus-bot giscus-bot 2022-12-16 18:20:15
Guest *Max* @ 2010-05-05 00:59:27 originally posted:

ok, so I reran the code and it looks like:

> library(gWidgetsRGtk2)
Loading required package: gWidgets
> (options(guiToolkit = "RGtk2"))
$guiToolkit
NULL

> ## if you don't want to install Gtk+, you may use the less fancy tcl/tk
> ## options(guiToolkit = 'tcltk')
> tbl = glayout(container = gwindow("Power of the F Test"),
+     spacing = 0)
> tbl[1, 1:4, anchor = c(0, 0), expand = TRUE] = g.f = ggraphics(container = tbl,
+     expand = TRUE, ps = 11)
Loading required package: cairoDevice

Error in handler(...) : could not find function "asCairoDevice"
In addition: Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called 'cairoDevice'

the code worked until the end where:

> p.Ftest()
Error in legend("topright", c(as.expression(substitute(F[list(df1, df2)] ~  : 
  unused argument(s) (border = NA)

scrolling around gives the same error again:
Error in legend("topright", c(as.expression(substitute(F[list(df1, df2)] ~  : 
  unused argument(s) (border = NA)

However: The slider controls did in fact pop up, so it's very interesting to see how this works. Now, I have a question for you, would it be possible to put this into an interface or function that directly calculates power as a function of df1, df2, delta (or delta^2) and alpha?

I ask because I often have to try and find a minimum sample size for ANOVA tests where typically df2=k*df1 given a specific power and alpha, but working "backwards" would also be just fine for me.

I'm curious about this, I have

yihui yihui 2022-12-16 18:20:16

Ahh... I should have mentioned about the cairoDevice package too, as the graphics widget depends on this package. I've modified my code above to (1) install the missing packages automatically (2) let the p.Ftest() return the value of power.

> p.Ftest(3, 20, 5, 0.05, 5)
[1] 0.3692085

Hopefully it is clear enough now.

Originally posted on 2010-05-05 06:42:49

giscus-bot giscus-bot 2022-12-16 18:20:10
Guest *Max* @ 2010-05-04 22:56:53 originally posted:

Here's what my output looks like, I realize this would be helpful to figure out what i did wrong! ( also I did install the relavent R packages. I think.

> library(gWidgetsRGtk2)
Loading required package: gWidgets
> options(guiToolkit = "RGtk2")
> ## if you don't want to install Gtk+, you may use the less fancy tcl/tk
> options(guiToolkit = 'tcltk')
> tbl = glayout(container = gwindow("Power of the F Test"),
+     spacing = 0)
Loading required package: gWidgetstcltk
Error in function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ".glayout", for signature "guiWidgetsToolkittcltk"
In addition: Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called 'gWidgetstcltk'
> tbl[1, 1:4, anchor = c(0, 0), expand = TRUE] = g.f = ggraphics(container = tbl,
+     expand = TRUE, ps = 11)
Loading required package: gWidgetstcltk
Error in function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ".ggraphics", for signature "guiWidgetsToolkittcltk"
In addition: Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called 'gWidgetstcltk'
> tbl[2, 1, anchor = c(1, 0)] = "numerator df"
Error in tbl[2, 1, anchor = c(1, 0)] = "numerator df" : 
  object 'tbl' not found
> tbl[2, 2, anchor = c(0, 0), expand = TRUE] = g.dfn = gslider(from = 1,
+     to = 50, value = 3, container = tbl, handler = function(h,
+         ...) {
+         p.Ftest(dfn = svalue(h$obj))
+     })
Loading required package: gWidgetstcltk
Error in function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ".gslider", for signature "guiWidgetsToolkittcltk"
In addition: Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called 'gWidgetstcltk'
> tbl[2, 3, anchor = c(1, 0)] = "denominator df"
Error in tbl[2, 3, anchor = c(1, 0)] = "denominator df" : 
  object 'tbl' not found
> tbl[2, 4, anchor = c(0, 0), expand = TRUE] = g.dfd = gslider(from = 1,
+     to = 50, value = 20, container = tbl, handler = function(h,
+         ...) {
+         p.Ftest(dfd = svalue(h$obj))
+     })
Loading required package: gWidgetstcltk
Error in function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ".gslider", for signature "guiWidgetsToolkittcltk"
In addition: Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called 'gWidgetstcltk'
> tbl[3, 1, anchor = c(1, 0)] = "delta^2"
Error in tbl[3, 1, anchor = c(1, 0)] = "delta^2" : object 'tbl' not found
> tbl[3, 2, anchor = c(0, 0), expand = TRUE] = g.ncp = gslider(from = 0,
+     to = 100, value = 10, container = tbl, handler = function(h,
+         ...) {
+         p.Ftest(ncp = svalue(h$obj))
+     })
Loading required package: gWidgetstcltk
Error in function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ".gslider", for signature "guiWidgetsToolkittcltk"
In addition: Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called 'gWidgetstcltk'
> tbl[3, 3, anchor = c(1, 0)] = "alpha"
Error in tbl[3, 3, anchor = c(1, 0)] = "alpha" : object 'tbl' not found
> tbl[3, 4, anchor = c(0, 0), expand = TRUE] = g.alpha = gslider(from = 0,
+     to = 1, by = 0.01, value = 0.05, container = tbl, handler = function(h,
+         ...) {
+         p.Ftest(alpha = svalue(h$obj))
+     })
Loading required package: gWidgetstcltk
Error in function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ".gslider", for signature "guiWidgetsToolkittcltk"
In addition: Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called 'gWidgetstcltk'
> tbl[4, 1, anchor = c(1, 0)] = "x range"
Error in tbl[4, 1, anchor = c(1, 0)] = "x range" : object 'tbl' not found
> tbl[4, 2:4, anchor = c(0, 0), expand = TRUE] = g.xr = gslider(from = 1,
+     to = 50, value = 15, container = tbl, handler = function(h,
+         ...) {
+         p.Ftest(xr = svalue(h$obj))
+     })
Loading required package: gWidgetstcltk
Error in function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ".gslider", for signature "guiWidgetsToolkittcltk"
In addition: Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called 'gWidgetstcltk'
> ## draw the graph
> p.Ftest = function(dfn = svalue(g.dfn), dfd = svalue(g.dfd),
+     ncp = svalue(g.ncp), alpha = svalue(g.alpha), xr = svalue(g.xr)) {
+     x = seq(0.001, xr, length.out = 300)
+     yc = df(x, dfn, dfd)
+     yn = df(x, dfn, dfd, ncp = ncp)
+     par(mar = c(4.5, 4, 1, 0.05))
+     plot(x, yc, type = "n", ylab = "Density", ylim = c(0, max(yc,
+         yn)))
+     xq = qf(1 - alpha, dfn, dfd)
+     polygon(c(xq, x[x >= xq], xr), c(0, yn[x > xq], 0), col = "gray",
+         border = NA)
+     lines(x, yc, lty = 1)
+     lines(x, yn, lty = 2)
+     legend("topright", c(as.expression(substitute(F[list(df1,
+         df2)] ~ " density", list(df1 = dfn, df2 = dfd))), as.expression(substitute(F[list(df1,
+         df2)](ncp) ~ " density", list(df1 = dfn, df2 = dfd, ncp = ncp))),
+         as.expression(substitute("Power = " ~ p, list(p = round(1 -
+             pf(xq, dfn, dfd, ncp = ncp), 4))))), lty = c(1:2,
+         NA), fill = c(NA, NA, "gray"), border = NA, bty = "n")
+ }
> p.Ftest()
Error: object 'g.xr' not found
Error in svalue(g.xr) : 
  error in evaluating the argument 'obj' in selecting a method for function 'svalue'
giscus-bot giscus-bot 2022-12-16 18:20:11
Guest *j verzani* @ 2010-05-10 04:40:23 originally posted:

Just for fun, something similar can be done using traitr, which gives an alternate way of using gWidgets. Here is how the above can look. (One could change the default view to arrange the sliders, but this keeps it simpler looking)

require(traitr)                         # also uses cairoDevice, gWidgetsRGtk2, gWidgets
options(guiToolkit = "RGtk2")           # not tcltk -- using ggraphics


drawGraph <- function(.) {
  vals = xq], xr), c(0, yn[x > xq], 0), col = "gray",
            border = NA)
    lines(x, yc, lty = 1)
    lines(x, yn, lty = 2)
    legend("topright", c(as.expression(substitute(F[list(df1,
        df2)] ~ " density", list(df1 = dfn, df2 = dfd))), as.expression(substitute(F[list(df1,
            df2)](ncp) ~ " density", list(df1 = dfn, df2 = dfd, ncp = ncp))),
                         as.expression(substitute("Power = " ~ p, list(p = round(1 -
                             pf(xq, dfn, dfd, ncp = ncp), 4))))), lty = c(1:2,
                                                                    NA), fill = c(NA, NA, "gray"), border = NA, bty = "n")
    return(1 - pf(xq, dfn, dfd, ncp = ncp))
  })
}

dlg <- aDialog(items=list(
                 graph=graphicDeviceItem(),
                 dfn=rangeItem(value=3, from=1, to=50, label="numerator d.f."),
                 dfd=rangeItem(value=20, from=1, to=50, label="denominator d.f."),
                 ncp=rangeItem(value=10, from=0, to=100, label="delta^2"),
                 alpha=rangeItem(value=0.05, from=0, to=1, by=0.01, label="alpha"),
                 xr = rangeItem(value=15, from=1, to=50, label="x range")
                 ),
               title="Power of the F Test",
               model_value_changed=drawGraph,
               Ok_handler=drawGraph,
               help_string="Change the value to see the power of the F test")

dlg$make_gui()                          # can define a view to adjust layout of widgets
drawGraph(dlg)                          # initial graphic
yihui yihui 2022-12-16 18:20:17

Sorry, you code seems to be messed up by my blog system (WordPress)... There is something wrong in the function drawGraph(). Anyway, I knew your traitr package when I was reading the abstracts of useR! 2010 -- that is very interesting and handy. Maybe we can meet in July :-)

Originally posted on 2010-05-11 07:35:27

giscus-bot giscus-bot 2022-12-16 18:20:18
Guest *j verzani* @ 2010-05-12 01:39:37 originally posted:

Oops, didn't see that. Basically my drawGraph is a wrapper for your function. Try this instead:

drawGraph <- function(.) do.call("p.Ftest", .$to_R())

See you at useR!

--John

Sign in to join the discussion

Sign in with GitHub