Reputation: 91
I have a classical linear model y ~ x1 + x2. How can I plot a heatmap of this model? Normally I print a heatmap from data in this way:
library(pheatmap)
data <- data.frame(y=c(1,2,3), x1=c(1.5,2,2.5), x2=c(3,10,12))
pheatmap(cor(data), scale="none")
But in this case I want to see the effect of y on x1 and x2. So I need a heatmap with a covariate for y. Something like this:
pheatmap(cor(select(data,-c(y))), scale="none", covariate=y)
But i didn't find a correct syntax for y as covariate. Please help me.
Upvotes: 1
Views: 98
Reputation: 22044
How about something like this (where y
is qsec
, x1
is hp
and x2
is wt
:
library(tidyr)
library(ggplot2)
data(mtcars)
mod <- lm(qsec ~ hp + wt, data=mtcars)
hp_s <- seq(min(mtcars$hp), max(mtcars$hp), length=25)
wt_s <- seq(min(mtcars$wt), max(mtcars$wt), length=25)
eg <- expand_grid(hp = hp_s, wt=wt_s)
eg$fit <- predict(mod, newdata=eg)
ggplot(eg, aes(x=hp, y=wt, fill=fit)) +
geom_tile() +
scale_fill_viridis_c() +
theme_classic()
Created on 2022-11-27 by the reprex package (v2.0.1)
Upvotes: 1