knit 和 by chunk 执行在 R Markdown 中有不同的结果

问题描述 投票:0回答:1

我在 RStudio 中工作,创建一个 R Markdown 文档,输出一些带有指标的表格。当我清除全局环境中的所有内容并一一执行块时,我得到了有效的结果。但是,当我编织文档时,每个表中的表都有相同的结果,就好像它不会重新计算每个块的值一样。

以下是完整代码:

---
title: "The Dataset"
output:
  pdf_document: default
  html_document:
    df_print: paged
date: "2024-06-18"
---
knitr::opts_chunk$set(echo = FALSE, cache=FALSE)

library(knitr)

# Load the CSV files
trends_data <- read.csv('quarterly_trends_averages.csv')
gdp_data <- read.csv('gdp moldova quarterly - Foaie1.csv')

# Rename the columns in trends_data
colnames(trends_data) <- c("Date", "Arts_Entertainment", "Autos_Vehicles", 
                           "Beauty_Fitness", "Business_Industrial", "Finance", 
                           "Food_Drink", "Home_Garden", "Jobs_Education", 
                           "Real_Estate", "Shopping")

# Rename the columns in gdp_data
names(gdp_data) <- c('Date', 'gdp', 'Ignore')

gdp_data$Ignore <- NULL

# Convert 'Date' to numeric in both datasets
trends_data$Date <- as.numeric(trends_data$Date)
gdp_data$Date <- as.numeric(gdp_data$Date)

# Merge the datasets on the 'Date' column
combined_data <- merge(trends_data, gdp_data, by = 'Date')

categories <- colnames(trends_data)[-1]  # Exclude the Date column


# Define metrics functions
mae <- function(actual, predicted) {
  return(round(mean(abs(actual - predicted)),3))
}

mse <- function(actual, predicted) {
  return(round(mean((actual - predicted)^2),3))
}

rmse <- function(actual, predicted) {
  return(round(sqrt(mean((actual - predicted)^2)),3))
}

mape <- function(actual, predicted) {
  return(round(mean(abs((actual - predicted) / actual)) * 100,3))
}

smape <- function(actual, predicted) {
  return(round(mean(2 * abs(actual - predicted) / (abs(actual) + abs(predicted))) * 100,3))
}

mase <- function(actual, predicted) {
  n <- length(actual)
  scale <- mean(abs(diff(actual)))
  return(round(mean(abs(actual - predicted)) / scale, 3))
}


模型:$GDP_t \sim GT_i,t$

metrics_df3 <- data.frame(
  Metric = c("Adjusted R^2", "MAE", "RMSE", "MAPE", "sMAPE", "MASE")
)

for (category in categories) {
  

df3 <- data.frame(
  y <- combined_data$gdp,
  x <- combined_data[[category]]
)


model <- lm(formula = y ~ x, data = df3)

actual <- df3$y
predicted <- predict(model, df3)


metrics <- c(
    summary(model)$r.squared,
    mae(actual, predicted),
    rmse(actual, predicted),
    mape(actual, predicted),
    smape(actual, predicted),
    mase(actual, predicted)
  )

metrics_df3[category] <- round(metrics,2)

}

colnames(metrics_df3) <- c("Metric","1","2","3","4","5","6","7","8","9","10")



kable(metrics_df3)

模型:$GT_t \sim GT_{i,t} + GT_{i,t-1}$

metrics_df4 <- data.frame(
  Metric = c("Adjusted R^2", "MAE", "RMSE", "MAPE", "sMAPE", "MASE")
)

for (category in categories) {
  

df4 <- data.frame(
  y <- combined_data$gdp,
  x1 <- combined_data[[category]],
  x2 <- combined_data[[paste0("lag1_",category)]]
)

colnames(df4) <- c("y", "x1", "x2")

df4 <- na.omit(df4)


model <- lm(formula = y ~ x1 + x2, data = df4)

actual <- df4$y
predicted <- predict(model, df4)


metrics <- c(
    summary(model)$r.squared,
    mae(actual, predicted),
    rmse(actual, predicted),
    mape(actual, predicted),
    smape(actual, predicted),
    mase(actual, predicted)
  )

metrics_df4[category] <- round(metrics,2)

}

colnames(metrics_df4) <- c("Metric","1","2","3","4","5","6","7","8","9","10")



kable(metrics_df4)

当我“运行全部”时,结果仍然有效。尝试设置

cache = FALSE
但没有改变。确保每个数据框都有不同的名称,但仍然没有帮助。 尝试使用“local = TRUE”在本地环境中进行每个表计算,但没有帮助。

在 .pdf 中,表具有相同的结果,而当我逐块运行代码时,表具有不同的结果。

r pdf r-markdown rstudio knitr
1个回答
0
投票

knitr
可以具有与“全部运行”按钮或
source()
命令不同的根目录。使用
knitr::opts_knit$set(root.dir = "C:/my/project/folder")
将根目录设置为您的项目路径,并检查它是否适用于
knitr::opts_knit$get("root.dir")

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.