增加使用相同IVs但不同DVs的不同模型的标准化残差列。

问题描述 投票:1回答:2

我想用Tidyverse的方法来解决以下问题......

我以钻石数据集(来自ggplot2)为例。

structure(list(carat = c(0.23, 0.21, 0.23, 0.29, 0.31), cut = structure(c(5L, 
4L, 2L, 4L, 2L), .Label = c("Fair", "Good", "Very Good", "Premium", 
"Ideal"), class = c("ordered", "factor")), color = structure(c(2L, 
2L, 2L, 6L, 7L), .Label = c("D", "E", "F", "G", "H", "I", "J"
), class = c("ordered", "factor")), clarity = structure(c(2L, 
3L, 5L, 4L, 2L), .Label = c("I1", "SI2", "SI1", "VS2", "VS1", 
"VVS2", "VVS1", "IF"), class = c("ordered", "factor")), depth = c(61.5, 
59.8, 56.9, 62.4, 63.3), table = c(55, 61, 65, 58, 58), price = c(326L, 
326L, 327L, 334L, 335L), x = c(3.95, 3.89, 4.05, 4.2, 4.34), 
    y = c(3.98, 3.84, 4.07, 4.23, 4.35), z = c(2.43, 2.31, 2.31, 
    2.63, 2.75)), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame"))

我想运行3个独立的线性回归,其中DV是不同的,但IVs保持不变,例如。

  • x ~深度+价格
  • y ~深度+价格
  • z ~深度+价格

我想在原始数据框中添加一列新的数据,其中包括每个模型对每个观测值的标准残差(即x_stdresiduals,y_stdresiduals,z_stdresiduals)。

r linear-regression tidyverse
2个回答
1
投票

一种粗暴的方法是

diamonds %>% 
  mutate(
    x_stdresiduals = lm(x ~ depth + price)$residuals,
    y_stdresiduals = lm(y ~ depth + price)$residuals,
    z_stdresiduals = lm(z ~ depth + price)$residuals
  )
© www.soinside.com 2019 - 2024. All rights reserved.