【问题标题】:Adding a regression line to scatterplot matrix in R [duplicate]在R中向散点图矩阵添加回归线[重复]
【发布时间】:2021-10-26 16:00:52
【问题描述】:

我有一个名为“保险”的数据,其中包含以下内容:

head(insurance)
  Race Fire Theft  Age Invol Income
1 10.0  6.2    29 60.4   0.0 11.744
2 22.2  9.5    44 76.5   0.1  9.323
3 19.6 10.5    36 73.5   1.2  9.948
4 17.3  7.7    37 66.9   0.5 10.656
5 24.5  8.6    53 81.4   0.7  9.730
6 54.0 34.1    68 52.6   0.3  8.231

我想构建一个散点图矩阵,并在每个散点图上拟合回归线。为了实现它,我需要在代码plot(insurance) 中添加一些东西吗?我试过输入代码abline(insurance,col="red"),但什么也没出现。

【问题讨论】:

标签: r


【解决方案1】:

我们可以这样做:

z <- as.matrix(df)
z
class(z)

pairs( z, panel=function(x,y){
  points(x,y)
  abline(lm(y~x), col='red')
  text(0,1.5,labels = paste('R2=',round((cor(x,y))^2,2)) ,col='red' )
})

【讨论】:

    猜你喜欢
    • 2020-04-07
    • 2015-01-27
    • 1970-01-01
    • 1970-01-01
    • 2016-02-24
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多