1 / 27

Computing for Research I Spring 2011

Computing for Research I Spring 2011. R: EDA and writing commands March 9. Primary Instructor: Elizabeth Garrett-Mayer. Some more on graphics. EDA: boxplot, stripchart hist 3-Dish plots image contour. contour and image plot. EDA (exploratory data analysis). Make figures!

beth
Télécharger la présentation

Computing for Research I Spring 2011

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computing for Research ISpring 2011 R: EDA and writing commands March 9 Primary Instructor: Elizabeth Garrett-Mayer

  2. Some more on graphics • EDA: • boxplot, stripchart • hist • 3-Dish plots • image • contour

  3. contour and image plot

  4. EDA (exploratory data analysis) • Make figures! • plot, boxplot, hist, etc. • Point estimates and confidence intervals • t.test: in addition to p-value, gives mean and confidence interval. • binom.test: estimate + one-sample test + confidence interval. Exact calculations.

  5. Binomial tests • binom.test: one-sample test + confidence interval. assumes null is p=0.50 unless specified. Exact calculations. • prop.test: tests that proportions are the same across groups. Chi-square-based. • fisher.test: tests that proportions are the same across groups. Exact calculations.

  6. Other basic tests • wilcox.test: ranksum test (2 groups) or signed rank. • kruskal.test: ranksum test (>= 2 groups) • shapiro.test: Shapiro-Wilk normality test • mantelhaen.test: Mantel-Haenszel test

  7. Creating commands in R • General syntax function.name <- function(x, y,… z=T, w=NULL) { # type in all the stuff you want the function to do . . # at the end, you usually return something return(z) } • choose a meaningful function name! • in above, • x, y would be required. • z and w have defaults and so are NOT required arguments

  8. When to create a function? • When you have something you want to do more than once • Can be a simple routine that you use regularly e.g. power calculation for an odds ratio based on fixed prevalence of ‘disease’ and varying prevalence of exposure • Can be a routine that you want to repeat over a large set of variables, yet is specific to a data analysis

  9. Very simple example • Trimmed mean trimmean <- function(x) { y <- sort(x) n <- length(y) y <- y[-c(1,n)] meany <- mean(y) return(meany) }

  10. Try it out z <- c(-20,1,2,4,7,9,50,100) mean(z) trimmean(z) z <- (1:100)^2 mean(z) trimmean(z)

  11. What about trimming more? # what about trimming more? trimmean <- function(x, ntrim=1) { y <- sort(x) n <- length(y) v1 <- 1:ntrim v2 <- (n-ntrim+1):n y <- y[-c(v1,v2)] meany <- mean(y) return(meany) }

  12. Checking out what your function is doing trimmean <- function(x, ntrim=1) { y <- sort(x) n <- length(y) v1 <- 1:ntrim v2 <- (n-ntrim+1):n print(c(v1,v2)) y <- y[-c(v1,v2)] meany <- mean(y) return(meany) }

  13. Try it again z <- c(-20,1,2,4,7,9,50,100) mean(z) trimmean(z, ntrim=2) z <- (1:100)^2 mean(z) trimmean(z, ntrim=10)

  14. common ones to have saved logit <- function(p) { return(log(p/(1-p))) } unlogit <- function(x) return(exp(x)/(1+exp(x))) oddsratio <- function(x,y) { tabi <- table(x,y) or <- (tabi[1,1]*tabi[2,2])/(tabi[1,2]*tabi[2,1]) return(or) } **note: no need to have “{“ and “} “if your function only has one line

  15. Example: Ford Study • Objectives. We conducted a community based cancer clinical trials education intervention in South Carolina (SC), which has high rates of cancer disparities. However, African Americans are less likely than other groups to participate in clinical trials. Low participation rates appear to be an outcome of negative trial perceptions. • Methods. We conducted the intervention at 10 sites in eight counties. The intervention consisted of a 30-minute cancer clinical trials educational presentation. It was a component of a larger 4-hour cancer education program. Pre- and post-intervention surveys were administered. The 7-item Fallowfield instrument was used to assess perceptions of cancer clinical trials. Fisher’s exact tests were used to compare the proportion of participants who changed their responses from pre-test to post-test.

  16. Example: Evaluating an Intervention to Improve Clinical Trial Perceptions among Racially Diverse Communities in South Carolina

  17. Create a function for estimating the proportions for all 7 items • Goal for EACH item: • estimate proportion changing from N/DK to Y • estimate proportion changing from Y to N/DK • estimate confidence intervals for proportions • test that proportions are different • plot each proportion and confidence interval on a graph • show p-value on figure

  18. Example table(prects[,1]) table(postcts[,1]) table(prects[,1], postcts[,1])

  19. Proportions of interest • P(N/DK to Y|N/DK) = c/(a+c) • P(Y to N/DK|Y) = b/(b+d)

  20. Example • Assume you have a table of values, tabi • What do we want to do with the table? aa <- tabi[1,1] bb <- tabi[2,1] cc <- tabi[1,2] dd <- tabi[2,2] t1 <- binom.test(bb, bb+dd ) # y to n/dk t2 <- binom.test(cc, aa+cc) # n/dk to y p <- fisher.test(tabi)$p.value

  21. Create a vector of output, with labels vectr <- c( t1$estimate, t1$conf.int[1], t1$conf.int[2], t2$estimate, t2$conf.int[1], t2$conf.int[2], p) names(vectr) <- c("p1","Lci1", “Uci1", "p2", "Lci2", "Uci2", "p") vectr <- round(vectr,4)

  22. Put it all together in a function twobytwo <- function(tabi) { aa <- tabi[1,1] bb <- tabi[2,1] cc <- tabi[1,2] dd <- tabi[2,2] t1 <- binom.test(bb, bb+dd) t2 <- binom.test(cc, aa+cc) p <- fisher.test(tabi)$p.value vectr <- c(t1$estimate, t1$conf.int[1], t1$conf.int[2], t2$estimate, t2$conf.int[1], t2$conf.int[2], p) names(vectr) <- c("p1","Lci1", "Uci1","p2","Lci2","Uci2","p") vectr <- round(vectr,4) return(vectr) }

  23. What about making the figure? • All of the results needed are already generated in the function and stored in vectr. • Just need to include where to put the results: • Step 1: set up a plotting area • Step 2: include points and lines commands within function

  24. Add options to function, code to function twobytwo.figure <- function(tabi, i=1, coll=1, diff = 0.2, plt=F) { ... if(plt==T) { points(c(i-diff,i+diff), vectr[c(1,4)], pch=16, cex=1.5, col=coll) lines(rep(i-diff,2), vectr[c(2,3)], lty=1, lwd=2, col=coll) lines(rep(i+diff,2), vectr[c(5,6)], lty=2, lwd=2, col=coll) ptext <- ifelse(p<0.0001,"<0.0001",as.character(round(p,4))) text(i,-0.1, labels=ptext) } ... What is diff? What is coll? Why plt=T or F?

  25. Set up plotting figure # set up plot par(mar=c(6.5,4,2,2)) plot(c(0.5,7.5), c(0,1), type="n", xaxt="n", xlab="", ylab="Proportion Changing") abline(h=c(0,1)) abline(v=seq(0.5,7.5,1), lty=3) labs <- paste("Item ",c(1,2,3,4,5,6,7)) mtext(labs, side=1, at=1:7, line=5) axis(1, at=(sort(rep(1:7,2))+rep(c(-0.2,0.2),7)), labels=rep(c("Y to N/DK","N/DK to Y"),7) , las=2, cex.axis=0.8) # add lines to figure for(i in 1:7) { tabi <- table(prects[,i], postcts[,i]) twobytwo.figure(tabi,i, plt=T, coll="darkgreen") }

More Related