- 113 Views
- Uploaded on
- Presentation posted in: General

Computing for Research I Spring 2011

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 - - - - - - - - - - - - - - - - - - - - - - - - - -

Computing for Research ISpring 2011

R: EDA and writing commands

March 9

Primary Instructor:

Elizabeth Garrett-Mayer

- EDA:
- boxplot, stripchart
- hist

- 3-Dish plots
- image
- contour

- 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.

- 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.

- 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

- 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 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

- Trimmed mean
trimmean <- function(x) {

y <- sort(x)

n <- length(y)

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

meany <- mean(y)

return(meany)

}

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?

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)

}

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)

}

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)

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

- 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.

- 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

table(prects[,1])

table(postcts[,1])

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

- P(N/DK to Y|N/DK) = c/(a+c)
- P(Y to N/DK|Y)= b/(b+d)

- 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

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)

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)

}

- 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

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 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")

}