从函数输出中提取值:在R中

时间:2016-05-11 13:50:10

标签: r

对于示例数据框:

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

我正在使用函数计算relative risk

#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,"]"))
    }
  }
}

首先创建xtab:

df$region <- factor(df$region)
result <- xtabs(weight ~ region + result, data=df)
result

然后使用该函数计算相对风险:

calcRelativeRisk(result,alpha=0.05)
[1] "category = a , relative risk =  1.26904794624327"
[1] "category = a ,  95 % confidence interval = [ 0.751148304223936 , 2.14402759189898 ]"

我想标记相对风险(&#34; RR&#34;)和置信区间&#34; RR_upper&#34;和&#34; RR_lower&#34;。这是因为我正在创建一个大表,这个代码运行在多个数据帧上。如何从R输出中提取这些值? (然后围绕它们等)。我认为我可以更改功能打印选项,但由于我没有创建该功能,我想知道是否还有其他方法?

1 个答案:

答案 0 :(得分:2)

最好的办法是将print语句更改为输出数据的内容。如果将calcRelativeRisk(result,alpha=0.05)包裹在strtypeof中,则会得到NULL。 print语句不保存值并返回NULL对象。

在print语句之后,在函数的最后一行添加一个命名向量:

c("relative_risk"=relativeRisk,  "lowervalue"=lowervalue, "uppervalue"=uppervalue)

这将返回一个长度为3的数字向量,然后您可以使用它来构建表格。

所以函数的底部看起来像这样:

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

要检索这些值,只需使用以下内容:

myValues <- calcRelativeRisk(result,alpha=0.05)