簡體   English   中英

R 中的回歸 - 截距錯誤的回歸線

[英]Regression in R - Regression line with wrong intercept

我有一個關於我在 R-Studio 中所做的回歸的問題。 我的統計 output 說它是 y 軸“-196”的截距

但是當我用

geom_smooth(method = 'lm', formula = y~x)

攔截和線路不可能是正確的。 我還檢查了 dataframe,它看起來不錯。

在此處輸入圖像描述

有誰知道這里出了什么問題?

非常感謝您的幫助

Dataframe:

.csv Dataframe

完整代碼:

 df_final %>%
  filter(!is.na(case_per_million), !is.na(kof)) %>%
  ggplot(aes(kof, case_per_million, fill = region)) +
  geom_point(color = "white",
             shape = 21, alpha = 0.7, size = 2) +
  geom_smooth(method = lm, na.rm = TRUE, fullrange = TRUE,
              aes(group = 1), color = "steelblue",
              show.legend = FALSE,
              se = FALSE) +
  theme_ipsum_ps(grid = "XY",
                 axis = TRUE,
                 ticks = TRUE,
                 axis_col = "grey20") +
  labs(x = "KOF Globalisation Index (in 2017)",
       y = "COVID-19 Fälle pro 100.000 Einwohner (log)",
       title = "COVID-19 Fälle pro 100.000 Einwohner vs KOF Globalisation Index" ) +
  scale_y_log10(label = function(x) scales::comma(x, accuracy = 1)) +
  scale_x_continuous(limits = c(0, 100)) +
  scale_fill_manual("Region",
                    values = c("#98064A",
                               "#2D8587",
                               "#4C5C78",
                               "#E04E4B",
                               "#662C68",
                               "#932834")) +
  theme(panel.grid = element_line(linetype = "dashed"),
        legend.position = c(1,0),
        legend.justification = c(1,0))

對於 Output 我使用:

    reg2 <- lm(case_per_million ~ kof,
           data = df_final)
summary(reg2)

tl; dr我認為您在 y 軸上添加了對數刻度,這意味着您最終擬合的是對數線性回歸而不是線性回歸。

在干凈的 R session 中,加載您的數據:

m1 <- lm(case_per_million ~ kof, data=df_reg)
coef(m1)
library(ggplot2)
gg0 <- ggplot(df_reg, aes(kof, case_per_million)) +
    geom_smooth(method="lm",fullrange=TRUE) +
    scale_x_continuous(limits=c(0,NA)) +
    geom_point()
gg1 <- gg0 + scale_y_log10()
library(cowplot)
plot_grid(gg0,gg1)

在此處輸入圖像描述

你的 x 和 y 軸可以翻轉。 盡管不知道其中包含的數據,但無法確定答案。

試試這個 plot 現在應該更有意義

plot (df$case_per_million ~ df$kof)
abline(lm(df$case_per_million ~ df$kof))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM