[英]linear model with `lm`: how to get prediction variance of sum of predicted values
[英]Prediction of 'mlm' linear model object from `lm()`
我有三個數據集:
響應-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中受支持不佳的部分。您擁有的模型類是“ mlm”,即“多個線性模型”,它不是標准的“ lm”類。 當您有一組共同的協變量/預測變量的(獨立)響應變量時,就會得到此結果。 盡管lm()
函數可以適合這種模型,但對於“ mlm”類而言, predict
方法很差。 如果您查看methods(predict)
,您將看到predict.mlm*
。 通常用“LM”類線性模型, predict.lm
被稱為當你調用predict
; 但是對於“ mlm”類,將調用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()
data.frame()
。 當我們想要獲得矩陣的數據幀時,這是必須的。 您可以比較以下兩者之間的區別:
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
相比,它是如此不同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.