Reputation: 8494
For an example dataframe:
df <- structure(list(region = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L), .Label = c("a", "b", "c", "d"), class = "factor"),
result = c(0L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L), weight = c(0.126,
0.5, 0.8, 1.5, 5.3, 2.2, 3.2, 1.1, 0.1, 1.3, 2.5)), .Names = c("region",
"result", "weight"), row.names = c(NA, 11L), class = "data.frame")
I am calculating the relative risk using the function:
#Relative risk function
calcRelativeRisk <- function(mymatrix,alpha=0.05,referencerow=2)
{
numrow <- nrow(mymatrix)
myrownames <- rownames(mymatrix)
for (i in 1:numrow)
{
rowname <- myrownames[i]
DiseaseUnexposed <- mymatrix[referencerow,1]
ControlUnexposed <- mymatrix[referencerow,2]
if (i != referencerow)
{
DiseaseExposed <- mymatrix[i,1]
ControlExposed <- mymatrix[i,2]
totExposed <- DiseaseExposed + ControlExposed
totUnexposed <- DiseaseUnexposed + ControlUnexposed
probDiseaseGivenExposed <- DiseaseExposed/totExposed
probDiseaseGivenUnexposed <- DiseaseUnexposed/totUnexposed
# calculate the relative risk
relativeRisk <- probDiseaseGivenExposed/probDiseaseGivenUnexposed
print(paste("category =", rowname, ", relative risk = ",relativeRisk))
# calculate a confidence interval
confidenceLevel <- (1 - alpha)*100
sigma <- sqrt((1/DiseaseExposed) - (1/totExposed) +
(1/DiseaseUnexposed) - (1/totUnexposed))
# sigma is the standard error of estimate of log of relative risk
z <- qnorm(1-(alpha/2))
lowervalue <- relativeRisk * exp(-z * sigma)
uppervalue <- relativeRisk * exp( z * sigma)
print(paste("category =", rowname, ", ", confidenceLevel,
"% confidence interval = [",lowervalue,",",uppervalue,"]"))
}
}
}
First creating the xtab:
df$region <- factor(df$region)
result <- xtabs(weight ~ region + result, data=df)
result
And then using the function to calculate relative risk:
calcRelativeRisk(result,alpha=0.05)
[1] "category = a , relative risk = 1.26904794624327"
[1] "category = a , 95 % confidence interval = [ 0.751148304223936 , 2.14402759189898 ]"
I want to label the relative risk ("RR") and the confidence intervals "RR_upper" and "RR_lower". This is because I am creating a large table with this code run over multiple dataframes. How can I extract these values from the R output? (and then round them etc.). I presume I could change the function print options but as I didn't create the function, I wondered whether there was another way?
Upvotes: 0
Views: 543
Reputation: 38500
The best thing to do would be to change the print statement into something that outputs data. If you wrap calcRelativeRisk(result,alpha=0.05)
in either str
or typeof
, you get NULL. The print statement does not save the values and returns a NULL object.
After the print statement, in the final line of the function, add a named vector:
c("relative_risk"=relativeRisk, "lowervalue"=lowervalue, "uppervalue"=uppervalue)
This will return a numeric vector of length 3 that you can then use to build your tables.
So the bottom of your function would look like this:
lowervalue <- relativeRisk * exp(-z * sigma)
uppervalue <- relativeRisk * exp( z * sigma)
# print values of interest
print(paste("category =", rowname, ", ", confidenceLevel,
"% confidence interval = [",lowervalue,",",uppervalue,"]"))
}
}
# return values of interest
c("relative_risk"=relativeRisk, "lowervalue"=lowervalue, "uppervalue"=uppervalue)
}
To retrieve these values simply use the following:
myValues <- calcRelativeRisk(result,alpha=0.05)
Upvotes: 2