R中的autoplot()表示lm:为什么我会得到一个;恒定杠杆:残差与因子水平”;而不是",;残差与杠杆比率”;情节

R中的autoplot()表示lm:为什么我会得到一个;恒定杠杆:残差与因子水平”;而不是",;残差与杠杆比率”;情节,r,r-markdown,lm,ancova,R,R Markdown,Lm,Ancova,我正在用一个连续变量(密度)和一个因子(季节)对R进行方差分析。当我检查模型假设时,我得到了一个名为“恒定杠杆:残差与因子水平”的图,而不是“残差与杠杆”的图 limp.mod如果没有可复制的示例,我将首先基于内置数据集iris创建一个模型 df1 <- iris[1:100, 3:5] df1$Species <- droplevels(df1$Species) str(df1) #'data.frame': 100 obs. of 3 variables: # $ Peta

我正在用一个连续变量(密度)和一个因子(季节)对R进行方差分析。当我检查模型假设时,我得到了一个名为“恒定杠杆:残差与因子水平”的图,而不是“残差与杠杆”的图


limp.mod如果没有可复制的示例,我将首先基于内置数据集
iris
创建一个模型

df1 <- iris[1:100, 3:5]
df1$Species <- droplevels(df1$Species)
str(df1)
#'data.frame':   100 obs. of  3 variables:
# $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
# $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
# $ Species     : Factor w/ 2 levels "setosa","versicolor": 1 1 1 1 1 1 1 1 1 1 ...

fit <- lm(Petal.Length ~ Petal.Width*Species, df1)

在哪个软件包中可以找到
autoplot
?使用非基本R函数时,请通过调用
library()
启动脚本。
library(ggplot2)library(dplyr)library(GGally)library(ggfortify)library(readr)
这些是我为此脚本加载的所有库。我认为autoplot()可以在ggplot2包中找到。实际上它在包
ggfortify
中。
df1 <- iris[1:100, 3:5]
df1$Species <- droplevels(df1$Species)
str(df1)
#'data.frame':   100 obs. of  3 variables:
# $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
# $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
# $ Species     : Factor w/ 2 levels "setosa","versicolor": 1 1 1 1 1 1 1 1 1 1 ...

fit <- lm(Petal.Length ~ Petal.Width*Species, df1)
library(ggplot2)

dflev <- data.frame(Leverage = hatvalues(fit), y = resid(fit))

ggplot(dflev, aes(Leverage, y)) +
  geom_point() +
  geom_hline(yintercept = 0, linetype = "dashed") +
  ggtitle("Residuals vs Leverage") +
  lims(y = c(-1, 1)) +
  ylab("") +
  theme(plot.title = element_text(hjust = 0.5, face = "bold"))