本文介绍了R中的最小二乘优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何解决R中的以下问题.
I am wondering how one could solve the following problem in R.
我们有一个 v 向量(包含 n 个元素)和一个 B 矩阵(维度为 mxn ) .例如:
We have a v vector (of n elements) and a B matrix (of dimension m x n).E.g:
> v
[1] 2 4 3 1 5 7
> B
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 2 1 5 5 3 4
[2,] 4 5 6 3 2 5
[3,] 3 7 5 1 7 6
我正在寻找 m 个长向量 u 这样
I am looking for the m-long vector u such that
sum( ( v - ( u %*% B) )^2 )
最小化(即最小化平方和).
is minimized (i.e. minimizes the sum of squares).
推荐答案
您正在描述线性回归,可以使用lm
函数来完成:
You are describing linear regression, which can be done with the lm
function:
coefficients(lm(v~t(B)+0))
# t(B)1 t(B)2 t(B)3
# 0.2280676 -0.1505233 0.7431653
这篇关于R中的最小二乘优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!