# get our data library("MASS") attach(Melanoma) # -------------------------------------------------------------------- # calculate mean and sd # -------------------------------------------------------------------- # calculate the means by outcome tapply(thickness, status, mean) # calculate the sds by outcome tapply(thickness, status, sd) # -------------------------------------------------------------------- # perform Chi-Square test # -------------------------------------------------------------------- # set up space in our matrix M <- matrix(nrow=2, ncol=3, dimnames=list(c("ulcer", "no ulcer"), c("died of melanoma", "survived", "died of other causes"))) # get our totals for the matrix M[["ulcer", "died of melanoma"]] <- length(which(ulcer==1 & status==1)) M[["ulcer", "survived"]] <- length(which(ulcer==1 & status==2)) M[["ulcer", "died of other causes"]] <- length(which(ulcer==1 & status==3)) M[["no ulcer", "died of melanoma"]] <- length(which(ulcer==0 & status==1)) M[["no ulcer", "survived"]] <- length(which(ulcer==0 & status==2)) M[["no ulcer", "died of other causes"]] <- length(which(ulcer==0 & status==3)) # run the mighty chi-square test print(chisq.test(M)) print(chisq.test(M, simulate.p.value=TRUE)) detach()