【问题标题】:Iris Residual Matrix虹膜残差矩阵
【发布时间】:2021-09-16 08:04:38
【问题描述】:

我正在尝试计算每类花瓣长度和宽度的残差矩阵 (2*ng),其条件是 R 上 Iris 数据集的萼片长度和宽度(ng = gth 类中的观察数)。我需要为每个班级将残差绘制为双变量图上的点。

我有以下代码,但觉得这不是正确的方法。任何帮助将不胜感激,谢谢!

fit = lm(data = iris, Petal.Length + Petal.Width ~ Sepal.Length + Sepal.Width)
iris$predicted = predict(fit)
iris$residuals = residuals(fit)
iris %>% select(predicted, residuals) %>% head()

ggplot(iris, aes(x=Sepal.Length, y=Petal.Length)) + geom_point() + geom_point(aes(y=predicted), shape = 1) + theme_bw()
ggplot(iris, aes(x=Sepal.Length, y=Petal.Width)) + geom_point() + geom_point(aes(y=predicted), shape = 1) + theme_bw()
ggplot(iris, aes(x=Sepal.Width, y=Petal.Length)) + geom_point() + geom_point(aes(y=predicted), shape = 1) + theme_bw()
ggplot(iris, aes(x=Sepal.Width, y=Petal.Width)) + geom_point() + geom_point(aes(y=predicted), shape = 1) + theme_bw()

【问题讨论】:

    标签: r


    【解决方案1】:

    从您的描述和代码中不太清楚您希望如何绘制残差。如果您想可视化预测值和实际值,只需选择您需要的变量:

    library(ggplot2)
    library(tidyr)
    
    iris %>% select(Sepal.Length,Species,Petal.Length,predicted) %>% 
    pivot_longer(-c(Sepal.Length,Species)) %>% 
    ggplot(aes(x = Sepal.Length,y=value,shape=name)) + 
    geom_point() +
    facet_wrap(~Species)
    

    【讨论】:

    • 我认为它需要 3 个单独的图表,分别用于该物种:Virginica、Versi Color 和 Setosa
    • 再次,您的代码和问题不是很清楚。看看更新是否正确
    猜你喜欢
    • 2020-10-05
    • 1970-01-01
    • 2018-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-26
    相关资源
    最近更新 更多