gpt4 book ai didi

r - 使用 lm 并预测矩阵中的数据

转载 作者:行者123 更新时间:2023-12-04 17:23:42 26 4
gpt4 key购买 nike

我用 R只使用一点点,从不使用数据框,这使得理解 predict 的正确使用变得困难。我的数据是普通矩阵,而不是数据框,称它们为 ab , 分别是 N x pM x p矩阵分别。我可以运行回归 lm(a[,1] ~ a[,-1]) .我想使用结果 lm预测对象b[,1]来自 b[,-1] .我对 predict(lm(a[,1] ~ a[,-1]), b[,-1]) 的天真猜测不起作用。使用 lm 的正确语法是什么?得到一个预测向量?

最佳答案

您可以将整个矩阵存储在 data.frame 的一列中。 :

x <- a [, -1]
y <- a [, 1]
data <- data.frame (y = y, x = I (x))
str (data)
## 'data.frame': 10 obs. of 2 variables:
## $ y: num 0.818 0.767 -0.666 0.788 -0.489 ...
## $ x: AsIs [1:10, 1:9] 0.916274.... 0.386565.... 0.703230.... -2.64091.... 0.274617.... ...

model <- lm (y ~ x)
newdata <- data.frame (x = I (b [, -1]))
predict (model, newdata)
## 1 2
## -3.795722 -4.778784

关于 pls 包的论文 ( Mevik, B.-H. and Wehrens, R. The pls Package: Principal Component and Partial Least Squares Regression in R Journal of Statistical Software, 2007, 18, 1 - 24. ) 解释了这种技术。

光谱数据集(奎宁荧光)的另一个例子是在 vignette ("flu") 中。我的包 hyperSpec。

关于r - 使用 lm 并预测矩阵中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15274039/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com