【问题标题】:Loess fit wrong in R, lines all over the place? [duplicate]黄土在R中不合适,到处都是线条? [复制]
【发布时间】:2017-06-14 16:03:03
【问题描述】:

我在 R 中遇到 Loess fit 的问题。我正在使用每个 R 安装中包含的默认 iris 数据集。但是,当我尝试制作黄土线时,它到处都是。 loess(y~x) 的 loess 线是红色的,因为我想试验一下,看看我是否对 x 和 y 进行了错误的排序,所以 loess(x~y) 的线是蓝色的。如您所见,这些行显然是错误的。为什么会这样?我似乎无法修复它。代码如下:

#ignore
library(lattice)
#cloud()

#CODE OF CONCERN BELOW
data <- iris
n<-150
x <- data$Petal.Length
y<-data$Petal.Width
plot(y ~ x)
loess_fit <- loess(y~x)
lines(x, predict(loess_fit), col = "blue")

这是我得到的图片:

【问题讨论】:

标签: r line loess


【解决方案1】:

使用黄土前需要先点数。

x <- sort(data$Petal.Length)
y<-data$Petal.Width[order(data$Petal.Length)] 
plot(y ~ x)
loess_fit <- loess(y~x)
lines(x, predict(loess_fit), col = "blue")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-18
    • 2022-01-23
    • 1970-01-01
    相关资源
    最近更新 更多