half-normal 的图绘制了 ggplot2 中的残差
graph of half-normal plots residuals in ggplot2
我正在尝试为 ggplot2 中 half-normal 图中的残差绘制下面的图表。
但是,我遇到了一个名为错误的错误:data
必须是一个数据帧,或者其他 object 可被 fortify ()
强制,而不是 S3 object class hnp.
下面可以看到数据
library(hnp)
Stones <- c(rep("Stone1",1), rep("Stone2",1), rep("Stone3",1))
treat <- c(rep("T6",9), rep("T7",9), rep("T8",9),
rep("T10",9), rep("T11",9),
rep("T12",9),rep("T14",9),rep("T15",9),
rep("T16",9))
Samples <- c(rep("SampleI",3), rep("SampleF",3), rep("SampleR",3))
Count <- c(3,3,1,1,2,2,0,0,0,1,2,3,0,0,1,0,0,0,2,0,2,2,0,0,0,0,0,6,
1,0,2,2,2,0,0,0,4,4,1,3,2,5,0,0,0,3,4,4,5,1,2,0,0,0,
4,4,6,1,2,1,0,0,0,4,6,4,2,3,1,0,0,0,3,2,2,1,0,5,0,1,0)
Total <- c(rep("6", 81))
dados <- data.frame(treat, Stones, Samples, Count, Total);dados
dados$treat = as.factor(dados$treat)
dados$Samples = as.factor(dados$Samples)
dados$Total = as.numeric(dados$Total)
dados$Prop = dados$Count/dados$Total
dados16 = dados[dados$treat=="T16",];dados16`
模型调整如下图
resp16 <-cbind(dados16$Count,dados16$Total - dados16$Count); resp16
m16 <- glm(resp16 ~ Samples,
data = dados16,
family = quasibinomial)
Graph16=hnp(m16, xlab = 'Percentil da N(0,1)', ylab = 'Resíduos',
main = 'Gráfico Normal de Probabilidades')
错误在这里。
ggplot(data = Graph16)
Erro: `data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class hnp
如错误所示,ggplot2 需要一个 data.frame
对象或一个已为其编写 fortify()
S3 方法的对象。 hnp
class 对象没有 fortify 方法,所以我们最好的选择可能是从 Graph16
对象中获取数据并在绘图之前将其放入数据框中。下面的示例假定 Graph16
对象与您的代码生成的对象相同。
library(ggplot2)
df <- data.frame(
x = Graph16$x,
lower = Graph16$lower,
median = Graph16$median,
upper = Graph16$upper,
residuals = Graph16$residuals
)
ggplot(df, aes(x)) +
geom_ribbon(aes(ymin = lower, ymax = upper),
alpha = 0.5) +
geom_point(aes(y = residuals)) +
geom_line(aes(y = median))
我正在尝试为 ggplot2 中 half-normal 图中的残差绘制下面的图表。
但是,我遇到了一个名为错误的错误:data
必须是一个数据帧,或者其他 object 可被 fortify ()
强制,而不是 S3 object class hnp.
下面可以看到数据
library(hnp)
Stones <- c(rep("Stone1",1), rep("Stone2",1), rep("Stone3",1))
treat <- c(rep("T6",9), rep("T7",9), rep("T8",9),
rep("T10",9), rep("T11",9),
rep("T12",9),rep("T14",9),rep("T15",9),
rep("T16",9))
Samples <- c(rep("SampleI",3), rep("SampleF",3), rep("SampleR",3))
Count <- c(3,3,1,1,2,2,0,0,0,1,2,3,0,0,1,0,0,0,2,0,2,2,0,0,0,0,0,6,
1,0,2,2,2,0,0,0,4,4,1,3,2,5,0,0,0,3,4,4,5,1,2,0,0,0,
4,4,6,1,2,1,0,0,0,4,6,4,2,3,1,0,0,0,3,2,2,1,0,5,0,1,0)
Total <- c(rep("6", 81))
dados <- data.frame(treat, Stones, Samples, Count, Total);dados
dados$treat = as.factor(dados$treat)
dados$Samples = as.factor(dados$Samples)
dados$Total = as.numeric(dados$Total)
dados$Prop = dados$Count/dados$Total
dados16 = dados[dados$treat=="T16",];dados16`
模型调整如下图
resp16 <-cbind(dados16$Count,dados16$Total - dados16$Count); resp16
m16 <- glm(resp16 ~ Samples,
data = dados16,
family = quasibinomial)
Graph16=hnp(m16, xlab = 'Percentil da N(0,1)', ylab = 'Resíduos',
main = 'Gráfico Normal de Probabilidades')
错误在这里。
ggplot(data = Graph16)
Erro: `data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class hnp
如错误所示,ggplot2 需要一个 data.frame
对象或一个已为其编写 fortify()
S3 方法的对象。 hnp
class 对象没有 fortify 方法,所以我们最好的选择可能是从 Graph16
对象中获取数据并在绘图之前将其放入数据框中。下面的示例假定 Graph16
对象与您的代码生成的对象相同。
library(ggplot2)
df <- data.frame(
x = Graph16$x,
lower = Graph16$lower,
median = Graph16$median,
upper = Graph16$upper,
residuals = Graph16$residuals
)
ggplot(df, aes(x)) +
geom_ribbon(aes(ymin = lower, ymax = upper),
alpha = 0.5) +
geom_point(aes(y = residuals)) +
geom_line(aes(y = median))