Markdown - 从文本中的 table 或列表调用结果

Markdown - call a result from a table or a list in the text

这是我正在尝试做的一个例子。我想在文本中调用 table 本身来自随机计算的结果。

---
title: "Régression logistique"
subtitle: "Analyse quantitative II"
author: 
- name: ""
- affiliation: 
date: "TP 4"
output: 
  html_document: 
    toc: yes
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r message=FALSE} 
bystander <- data.frame(reaction = c(rep(0, 30), rep(1, 30)),
                        age.victime = c(rep(5, 5),rep(85, 5),rep(7, 5),rep(35, 5),rep(45, 5),rep(50, 5)),
                        sexe.victime = c(rep(0, 20), rep(1, 10), rep(0, 10), rep(1, 20)),
                        nbrpers = c(11:40, 0:29),
                        statutse = c(rep(4, 30), rep(18, 30)))
 
#install.packages("caret")
library(caret)
# install.packages("lmtest")
library(lmtest)
#install.packages("pscl")
library(pscl)
#install.packages("e1071")
library(e1071)
```

```{r}
Train <- createDataPartition(bystander$reaction, p=0.8, list=FALSE)
training <- bystander[ Train, ]
testing <- bystander[ -Train, ]
training$reaction <- as.factor (training$reaction)
model_fit <- train(reaction ~ age.victime + sexe.victime + nbrepers + statutse, data=training, method="glm")
testing$reaction <- as.factor (testing$reaction)
pred <- predict(model_fit, newdata=testing)
confusionMatrix(data=pred, testing$reaction)
```

We obtain a an accuracy of `r confusionMatrix(data=pred, testing$reaction)`.

我只想要 confusionMatrix 中整体的准确度。有没有办法只调用这个结果,因为我的试验在那里不起作用(很合逻辑,它是一个列表),就像在乳胶中一样,当你可以参考你的结果并稍后调用它时。

提前致谢!

为实现您想要的结果,请将调用 caret::confusionMatrix 的结果分配给变量。之后,您可以像这样在内联块中访问 Accuracy

---
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r}
library(caret)
```

```{r}
conf_mat <- caret::confusionMatrix(iris$Species, sample(iris$Species))
```

We obtain an accuracy of `r conf_mat$overall[["Accuracy"]]`.