slide1 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Computing for Research I Spring 2013 PowerPoint Presentation
Download Presentation
Computing for Research I Spring 2013

Loading in 2 Seconds...

play fullscreen
1 / 27

Computing for Research I Spring 2013 - PowerPoint PPT Presentation


  • 87 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Computing for Research I Spring 2013' - nansen


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
slide1

Computing for Research ISpring 2013

R: EDA and writing commands

March 9

Primary Instructor:

Elizabeth Garrett-Mayer

some more on graphics
Some more on graphics
  • 3-Dish plots
    • image
    • contour
eda exploratory data analysis
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.
binomial tests
Binomial tests
  • binom.test: one-sample test and 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.
other basic tests
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
creating commands in r
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
when to create a function
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
very simple example
Very simple example
  • Trimmed mean

trimmean <- function(x) {

y <- sort(x)

n <- length(y)

y <- y[-c(1,n)]

meany <- mean(y)

return(meany)

}

try it out
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)

what about trimming more
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)

}

checking out what your function is doing
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)

}

try it again
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)

common ones to have saved
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

example ford study
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.
slide17
Example: Evaluating an Intervention to Improve Clinical Trial Perceptions among Racially Diverse Communities in South Carolina
create a function for estimating the proportions for all 7 items
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
example
Example

table(prects[,1])

table(postcts[,1])

table(prects[,1], postcts[,1])

proportions of interest
Proportions of interest
  • P(N/DK to Y|N/DK) = c/(a+c)
  • P(Y to N/DK|Y) = b/(b+d)
example1
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

tab2 <- matrix( c(aa, dd, cc, bb), byrow=F, ncol=2)

p <- fisher.test(tab2)$p.value

create a vector of output with labels
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)

put it all together in a function
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)

tab2 <- matrix( c(aa, dd, cc, bb), byrow=F, ncol=2)

p <- fisher.test(tab2)$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)

}

what about making the figure
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
add options to function code to function
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?

set up plotting figure
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")

}