Paquete pwr

Ejemplos ANOVA

Leemos un estudio y queremos saber si tiene la potencia suficiente

Sabemos el N, la diferencia, pero desconocemos el poder

pwr.anova.test(
  k = 2,
  n = 8, 
  f = 0.6, 
  sig.level = 0.05, 
  power = NULL # lo que deseamos despejar
)

     Balanced one-way analysis of variance power calculation 

              k = 2
              n = 8
              f = 0.6
      sig.level = 0.05
          power = 0.6078185

NOTE: n is number in each group

Conclusión: el estudio tiene una muestra menor a la requerida. Es muy probable que haya un error tipo II en los resultados

Estamos planificando un estudio

Sabemos la diferencia clínicamente significativa y el poder. ¿Cuántos pacientes incluir?

pwr.anova.test(
  k = 2,
  n = NULL,  # lo que deseamos despejar
  f = 0.6, 
  sig.level = 0.05, 
  power = 0.8
)

     Balanced one-way analysis of variance power calculation 

              k = 2
              n = 11.94226
              f = 0.6
      sig.level = 0.05
          power = 0.8

NOTE: n is number in each group

necesitamos once pacientes por grupo

Diferencias entre poblacion y muestra

table(muestra2)
muestra2
Enfermos    Sanos 
      32       68 
prop.test(s,t)

    2-sample test for equality of proportions with continuity correction

data:  s out of t
X-squared = 2.0247, df = 1, p-value = 0.1548
alternative hypothesis: two.sided
95 percent confidence interval:
 -0.03006994  0.17006994
sample estimates:
prop 1 prop 2 
  0.32   0.25 

ahora para una prevalencia del 10%

table(poblacion)
poblacion
Enfermos    Sanos 
     100      900 
table(muestra2)
muestra2
Enfermos    Sanos 
       1        9 
prop.test(s,t)

    2-sample test for equality of proportions with continuity correction

data:  s out of t
X-squared = 2.0247, df = 1, p-value = 0.1548
alternative hypothesis: two.sided
95 percent confidence interval:
 -0.03006994  0.17006994
sample estimates:
prop 1 prop 2 
  0.32   0.25 

Ejemplo del cambio de n para proporciones

Muestra pequeña

addmargins(datos)
        Adhesivas No adhesivas Sum
Éxito          18           15  33
Fracaso         2            4   6
Sum            20           19  39
chisq.test(datos) #NS
Chi-squared approximation may be incorrect

    Pearson's Chi-squared test with Yates' continuity correction

data:  datos
X-squared = 0.26241, df = 1, p-value = 0.6085

Muestra grande, pero con la misma proporción

addmargins(datos)
        Adhesivas No adhesivas Sum
Éxito         180          150 330
Fracaso        20           40  60
Sum           200          190 390
chisq.test(datos) #SIG

    Pearson's Chi-squared test with Yates' continuity correction

data:  datos
X-squared = 8.3142, df = 1, p-value = 0.003934

Tamaño muestral y poder

Proporción

pwr.p.test(
        h = 0.2,
        n = 20,
        sig.level = 0.05,
        power = NULL, 
        alternative = "two.sided"
        )

     proportion power calculation for binomial distribution (arcsine transformation) 

              h = 0.2
              n = 20
      sig.level = 0.05
          power = 0.1454725
    alternative = two.sided
pwr.p.test(
        h = 0.2,
        n = NULL, 
        sig.level = 0.05, 
        power = 0.80,
        alternative = "two.sided"
        )

     proportion power calculation for binomial distribution (arcsine transformation) 

              h = 0.2
              n = 196.2215
      sig.level = 0.05
          power = 0.8
    alternative = two.sided

Medias

dos grupos

Dos grupos independientes

pwr.t.test(
        n = NULL,
        d = 0.5,
        sig.level = 0.05,
        power = 0.8,
        type = c("two.sample")
)

     Two-sample t test power calculation 

              n = 63.76561
              d = 0.5
      sig.level = 0.05
          power = 0.8
    alternative = two.sided

NOTE: n is number in *each* group

Dos grupos apareados

pwr.t.test(
        n = NULL,
        d = 0.5,
        sig.level = 0.05,
        power = 0.8,
        type = c("paired")
)

     Paired t test power calculation 

              n = 33.36713
              d = 0.5
      sig.level = 0.05
          power = 0.8
    alternative = two.sided

NOTE: n is number of *pairs*

más de dos grupos

pwr.anova.test(
        k = 3,
        n= 10,
        f=0.25,
        power = NULL, 
        sig.level = 0.05
        )

     Balanced one-way analysis of variance power calculation 

              k = 3
              n = 10
              f = 0.25
      sig.level = 0.05
          power = 0.1951401

NOTE: n is number in each group
pwr.anova.test(
        k = 3,
        n = NULL,
        f = 0.25,
        power = 0.8, 
        sig.level = 0.05
        )

     Balanced one-way analysis of variance power calculation 

              k = 3
              n = 52.3966
              f = 0.25
      sig.level = 0.05
          power = 0.8

NOTE: n is number in each group

Muestras pequeñas

install.packages("statmod")
Installing package into ‘/home/sergiouribe/R/x86_64-pc-linux-gnu-library/3.4’
(as ‘lib’ is unspecified)
probando la URL 'https://cran.rstudio.com/src/contrib/statmod_1.4.30.tar.gz'
Content type 'application/x-gzip' length 57309 bytes (55 KB)
==================================================
downloaded 55 KB

* installing *source* package ‘statmod’ ...
** package ‘statmod’ successfully unpacked and MD5 sums checked
** libs
gfortran   -fpic  -g -O2 -fstack-protector --param=ssp-buffer-size=4  -c gaussq2.f -o gaussq2.o
gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG      -fpic  -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -g  -c init.c -o init.o
gcc -std=gnu99 -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o statmod.so gaussq2.o init.o -lgfortran -lm -lquadmath -L/usr/lib/R/lib -lR
installing to /home/sergiouribe/R/x86_64-pc-linux-gnu-library/3.4/statmod/libs
** R
** data
** inst
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (statmod)

The downloaded source packages are in
    ‘/tmp/RtmpzH45u9/downloaded_packages’
power.fisher.test(0.5,0.9,20,20)
[1] 0.78
LS0tCnRpdGxlOiAiUG9ibGFjacOzbiwgbXVlc3RyYSB5IHRhbWHDsW8gbXVlc3RyYWwiCm91dHB1dDogCiAgaHRtbF9ub3RlYm9vazogCiAgICB0aGVtZTogeWV0aQogICAgdG9jOiB0cnVlCiAgICB0b2NfZmxvYXQ6IHRydWUKLS0tCgojIFBhcXVldGUgcHdyCmBgYHtyfQpsaWJyYXJ5KHB3cikKYGBgCgojIEVqZW1wbG9zIEFOT1ZBCgojIyBMZWVtb3MgdW4gZXN0dWRpbyB5IHF1ZXJlbW9zIHNhYmVyIHNpIHRpZW5lIGxhIHBvdGVuY2lhIHN1ZmljaWVudGUKU2FiZW1vcyBlbCBOLCBsYSBkaWZlcmVuY2lhLCBwZXJvIGRlc2Nvbm9jZW1vcy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BgYHtyfQoKcHdyLnAudGVzdCgKICAgICAgICBoID0gMC4yLAogICAgICAgIG4gPSBOVUxMLCAKICAgICAgICBzaWcubGV2ZWwgPSAwLjA1LCAKICAgICAgICBwb3dlciA9IDAuODAsCiAgICAgICAgYWx0ZXJuYXRpdmUgPSAidHdvLnNpZGVkIgogICAgICAgICkKCmBgYAoKCiMjIE1lZGlhcwoKIyMjIGRvcyBncnVwb3MKCiMjIyMgRG9zIGdydXBvcyBpbmRlcGVuZGllbnRlcwoKCmBgYHtyfQpwd3IudC50ZXN0KAogICAgICAgIG4gPSBOVUxMLAogICAgICAgIGQgPSAwLjUsCiAgICAgICAgc2lnLmxldmVsID0gMC4wNSwKICAgICAgICBwb3dlciA9IDAuOCwKICAgICAgICB0eXBlID0gYygidHdvLnNhbXBsZSIpCikKYGBgCgojIyMjIERvcyBncnVwb3MgYXBhcmVhZG9zCmBgYHtyfQpwd3IudC50ZXN0KAogICAgICAgIG4gPSBOVUxMLAogICAgICAgIGQgPSAwLjUsCiAgICAgICAgc2lnLmxldmVsID0gMC4wNSwKICAgICAgICBwb3dlciA9IDAuOCwKICAgICAgICB0eXBlID0gYygicGFpcmVkIikKKQpgYGAKCgojIyMgbcOhcyBkZSBkb3MgZ3J1cG9zCgpgYGB7cn0KcHdyLmFub3ZhLnRlc3QoCiAgICAgICAgayA9IDMsCiAgICAgICAgbiA9IDEwLAogICAgICAgIGYgPSAwLjI1LAogICAgICAgIHBvd2VyID0gTlVMTCwgCiAgICAgICAgc2lnLmxldmVsID0gMC4wNQogICAgICAgICkKCmBgYAoKYGBge3J9CnB3ci5hbm92YS50ZXN0KAogICAgICAgIGsgPSAzLAogICAgICAgIG4gPSBOVUxMLAogICAgICAgIGYgPSAwLjI1LAogICAgICAgIHBvd2VyID0gMC44LCAKICAgICAgICBzaWcubGV2ZWwgPSAwLjA1CiAgICAgICAgKQoKYGBgCgojIE11ZXN0cmFzIHBlcXVlw7FhcwoKYGBge3J9CmxpYnJhcnkoc3RhdG1vZCkKYGBgCgpgYGB7cn0KP3Bvd2VyLmZpc2hlci50ZXN0CgpgYGAKCgoKCgo=