【问题标题】:Prediction of 'mlm' linear model object from `lm()`从“lm()”预测“mlm”线性模型对象
【发布时间】:2016-10-09 00:15:09
【问题描述】:

我有三个数据集:

响应 - 5(样本)x 10(因变量)矩阵

预测变量 - 5(样本)x 2(自变量)矩阵

test_set - 10(样本)x 10(响应中定义的因变量)矩阵

response <- matrix(sample.int(15, size = 5*10, replace = TRUE), nrow = 5, ncol = 10)
colnames(response) <- c("1_DV","2_DV","3_DV","4_DV","5_DV","6_DV","7_DV","8_DV","9_DV","10_DV") 
predictors <- matrix(sample.int(15, size = 7*2, replace = TRUE), nrow = 5, ncol = 2)
colnames(predictors) <- c("1_IV","2_IV")
test_set <- matrix(sample.int(15, size = 10*2, replace = TRUE), nrow = 10, ncol = 2)
colnames(test_set) <- c("1_IV","2_IV")

我正在使用定义为响应集和预测集的组合的训练集进行多元线性模型,我想使用该模型对测试集进行预测:

training_dataframe <- data.frame(predictors, response)
fit <- lm(response ~ predictors, data = training_dataframe)
predictions <- predict(fit, data.frame(test_set))

但是,预测的结果真的很奇怪:

predictions

首先,矩阵尺寸为 5 x 10,即响应变量中的样本数乘以 DV 的数量。

我对 R 中的这种类型的分析不是很熟练,但我不应该得到一个 10 x 10 的矩阵,以便我对我的 test_set 中的每一行都有预测吗?

对于这个问题的任何帮助将不胜感激, 马丁

【问题讨论】:

    标签: r regression linear-regression lm mlm


    【解决方案1】:

    您正在进入 R 中支持不佳的部分。您拥有的模型类是“mlm”,即“多重线性模型”,它不是标准的“lm”类。当您有一组共同的协变量/预测变量的多个(独立)响应变量时,您就会得到它。虽然lm() 函数可以适合这样的模型,但predict 方法对于“mlm”类来说很差。如果您查看methods(predict),您会看到predict.mlm*。通常对于具有“lm”类的线性模型,当您调用predict 时会调用predict.lm;但对于“传销”类,则调用 predict.mlm*

    predict.mlm* 太原始了。它不允许se.fit,即它不能产生预测误差、置信度/预测区间等,尽管这在理论上是可能的。它只能计算预测均值。如果是这样,我们为什么要使用predict.mlm*?!预测均值可以通过简单的矩阵-矩阵乘法获得(在标准“lm”类中,这是矩阵-向量乘法),因此我们可以自己完成。

    考虑这个小的,重现的例子。

    set.seed(0)
    ## 2 response of 10 observations each
    response <- matrix(rnorm(20), 10, 2)
    ## 3 covariates with 10 observations each
    predictors <- matrix(rnorm(30), 10, 3)
    fit <- lm(response ~ predictors)
    
    class(fit)
    # [1] "mlm" "lm"
    
    beta <- coef(fit)
    #                  [,1]       [,2]
    #(Intercept)  0.5773235 -0.4752326
    #predictors1 -0.9942677  0.6759778
    #predictors2 -1.3306272  0.8322564
    #predictors3 -0.5533336  0.6218942
    

    当你有一个预测数据集时:

    # 2 new observations for 3 covariats
    test_set <- matrix(rnorm(6), 2, 3)
    

    我们首先需要填充一个拦截列

    Xp <- cbind(1, test_set)
    

    然后做这个矩阵乘法

    pred <- Xp %*% beta
    #          [,1]      [,2]
    #[1,] -2.905469  1.702384
    #[2,]  1.871755 -1.236240
    

    也许你已经注意到我在这里甚至没有使用数据框。 是的,没有必要,因为您拥有矩阵形式的所有内容。 对于那些 R 向导,使用 lm.fit 甚至 qr.solve 可能更直接。


    但作为一个完整的答案,必须演示如何使用predict.mlm 来获得我们想要的结果。

    ## still using previous matrices
    training_dataframe <- data.frame(response = I(response), predictors = I(predictors))
    fit <- lm(response ~ predictors, data = training_dataframe)
    newdat <- data.frame(predictors = I(test_set))
    pred <- predict(fit, newdat)
    #          [,1]      [,2]
    #[1,] -2.905469  1.702384
    #[2,]  1.871755 -1.236240
    

    当我使用data.frame() 时,请注意I()。当我们想要获得矩阵的数据框时,这是必须的。您可以比较以下之间的区别:

    str(data.frame(response = I(response), predictors = I(predictors)))
    #'data.frame':  10 obs. of  2 variables:
    # $ response  : AsIs [1:10, 1:2] 1.262954.... -0.32623.... 1.329799.... 1.272429.... 0.414641.... ...
    # $ predictors: AsIs [1:10, 1:3] -0.22426.... 0.377395.... 0.133336.... 0.804189.... -0.05710.... ...
    
    str(data.frame(response = response, predictors = predictors))
    #'data.frame':  10 obs. of  5 variables:
    # $ response.1  : num  1.263 -0.326 1.33 1.272 0.415 ...
    # $ response.2  : num  0.764 -0.799 -1.148 -0.289 -0.299 ...
    # $ predictors.1: num  -0.2243 0.3774 0.1333 0.8042 -0.0571 ...
    # $ predictors.2: num  -0.236 -0.543 -0.433 -0.649 0.727 ...
    # $ predictors.3: num  1.758 0.561 -0.453 -0.832 -1.167 ...
    

    如果没有I()保护矩阵输入,数据就会乱七八糟。令人惊讶的是,这不会对lm 造成问题,但是如果不使用I()predict.mlm 将很难获得正确的预测矩阵。

    好吧,在这种情况下,我建议使用“列表”而不是“数据框”。 lm 中的 data 参数以及 predict 中的 newdata 参数允许列表输入。 “列表”是一种比数据框更通用的结构,它可以毫无困难地保存任何数据结构。我们可以这样做:

    ## still using previous matrices
    training_list <- list(response = response, predictors = predictors)
    fit <- lm(response ~ predictors, data = training_list)
    newdat <- list(predictors = test_set)
    pred <- predict(fit, newdat)
    #          [,1]      [,2]
    #[1,] -2.905469  1.702384
    #[2,]  1.871755 -1.236240
    

    也许最后,我应该强调使用公式接口而不是矩阵接口总是安全的。我将使用 R 内置数据集trees 作为可重现的示例。

    fit <- lm(cbind(Girth, Height) ~ Volume, data = trees)
    
    ## use the first two rows as prediction dataset
    predict(fit, newdata = trees[1:2, ])
    #     Girth   Height
    #1 9.579568 71.39192
    #2 9.579568 71.39192
    

    也许你还记得我说过predict.mlm* 太原始,无法支持se.fit。这是测试它的机会。

    predict(fit, newdata = trees[1:2, ], se.fit = TRUE)
    #Error in predict.mlm(fit, newdata = trees[1:2, ], se.fit = TRUE) : 
    #  the 'se.fit' argument is not yet implemented for "mlm" objects
    

    糟糕...置信/预测区间如何(实际上,如果没有计算标准误差的能力,就不可能产生这些区间)?好吧,predict.mlm* 会忽略它。

    predict(fit, newdata = trees[1:2, ], interval = "confidence")
    #     Girth   Height
    #1 9.579568 71.39192
    #2 9.579568 71.39192
    

    所以这与predict.lm 相比有很大不同。

    【讨论】:

      猜你喜欢
      • 2012-08-18
      • 1970-01-01
      • 1970-01-01
      • 2013-10-22
      • 2021-12-14
      • 2017-02-19
      • 1970-01-01
      • 1970-01-01
      • 2018-10-22
      相关资源
      最近更新 更多