我想做一个相关图,但是它不使用相关系数,而是显示每个变量之间线性回归的斜率。
如果可能的话,它将做同样的功能,因为它将显示哪个斜率是显着的或不重要的。对于变量之间的比较问题,我想最好是将所有的斜率标准化。
我想这样做,因为我有时有一个坏的相关性/R2,但仍然有一个很大的斜率。因此,同时拥有相关矩阵和“斜率”矩阵将是很好的。
你知道有没有这样的现有功能吗?或者怎么做?谢谢。
编辑:这里有一个链接解释了为什么斜率和R2/相关性之间有差异:https://statisticsbyjim.com/regression/low-r-squared-regression/
这里有一个例子,说明了我如何使用corr图。我想做的是一个类似的函数,但是用斜率,而不是相关性。
M<-cor(mtcars)
test <- cor.mtest(M, conf.level = 0.95)
corrplot(M, order="hclust", tl.col="black",
p.mat = test$p, sig.level = 0.10)发布于 2022-09-02 10:20:37
在这里,您有最适合的点(下面板)和回归参数(上面板):
#Panel of correlations
panel.corr <- function(x, y,data){
usr <- par("usr"); on.exit(par(usr))
par(usr = c(0, 1, 0, 1))
a <- round(summary(lm(x~y, data=mtcars))$coef[1,1],3)
b <- round(summary(lm(x~y, data=mtcars))$coef[2,1],3)
txt <- paste0("y=", a," + (",b,")*x")
text(0.5, 0.5, txt, cex = 1)
}
#Panel of histograms
panel.hist <- function(x, ...){
usr <- par("usr"); on.exit(par(usr))
par(usr = c(usr[1:2], 0, 1.5) )
h <- hist(x, plot = FALSE)
breaks <- h$breaks
len <- length(breaks)
y <- h$counts/max(h$counts)
rect(breaks[-len], 0, breaks[-1], y, col = "lightblue")
}
panel.scat <- function(x, y, ...) {
usr <- par("usr"); on.exit(par(usr))
par(usr = c(0, 1, 0, 1), new = TRUE)
plot(x,y)
abline(lm(y ~ x))
}
#Plot
pairs(mtcars[, c(1,3:7)],
lower.panel = panel.scat,
upper.panel = panel.corr,
diag.panel = panel.hist,
gap = 0.3,
main = "Scatterplot matrix of `mtcars`")发布于 2022-08-23 13:54:38
以下是关于本页面的教程并回答您的问题:
library(tidyverse)
library(ggpubr)
theme_set(theme_pubr())
# Load the package
data("marketing", package = "datarium")
head(marketing, 4)
ggplot(marketing, aes(x = youtube, y = sales)) +
geom_point() +
stat_smooth()
cor(marketing$sales, marketing$youtube)
model <- lm(sales ~ youtube, data = marketing)
model调用模型的输出是:
##
## Call:
## lm(formula = sales ~ youtube, data = marketing)
##
## Coefficients:
## (Intercept) youtube
## 8.4391 0.0475你要找的信息是:
如果您只想与您先前计算的特性进行比较,只需将其转换为公式,您就会得到一个简单的回归模型。我必须建议你检查线性回归的先决条件,然后以防万一.
希望能帮上忙。
https://stackoverflow.com/questions/73459876
复制相似问题