根据类型创建具有最小二乘回归线的散点图
Creating a scatter diagram with least square regression line according to Type
我有如下设置的数据:
type age rust
0 1 29.00
0 0 95.00
.....
1 1800 7.56
我想根据类型 0 和类型 1 创建生锈与年龄的散点图。我还想为类型 0 覆盖一条红色的最小二乘回归线,为类型 1 覆盖蓝色。
我尝试了以下操作,但是当我尝试覆盖 abline 命令时,它偏离了图表:
library(lattice)
xyplot(rust$rust~rust$age, group = rust$type, main = "Scatterplot of Rust vs. Age by Type")
abline(rust$rust~rust$age, col = "blue")
根据您的具体数据详细说明我的评论,这样的事情应该可行。
require(lattice)
set.seed(42)
type <- rep(c(0,1),20)
age <- sample(1:100, 40, replace=TRUE)
rust <- type*5 + 0.3 * age + rnorm(40)
df <- data.frame(type = type, age = age, rust = rust)
myPanel <- function(x,y,...) {
panel.xyplot(x,y,...)
panel.abline(lm(y~x), ...)
}
xyplot(rust ~ age, group = type, data = df, panel = panel.superpose,
panel.groups = myPanel)
我有如下设置的数据:
type age rust
0 1 29.00
0 0 95.00
.....
1 1800 7.56
我想根据类型 0 和类型 1 创建生锈与年龄的散点图。我还想为类型 0 覆盖一条红色的最小二乘回归线,为类型 1 覆盖蓝色。
我尝试了以下操作,但是当我尝试覆盖 abline 命令时,它偏离了图表:
library(lattice)
xyplot(rust$rust~rust$age, group = rust$type, main = "Scatterplot of Rust vs. Age by Type")
abline(rust$rust~rust$age, col = "blue")
根据您的具体数据详细说明我的评论,这样的事情应该可行。
require(lattice)
set.seed(42)
type <- rep(c(0,1),20)
age <- sample(1:100, 40, replace=TRUE)
rust <- type*5 + 0.3 * age + rnorm(40)
df <- data.frame(type = type, age = age, rust = rust)
myPanel <- function(x,y,...) {
panel.xyplot(x,y,...)
panel.abline(lm(y~x), ...)
}
xyplot(rust ~ age, group = type, data = df, panel = panel.superpose,
panel.groups = myPanel)