Least square optimization in R

房东的猫 提交于 2019-12-24 04:08:24

问题


I am wondering how one could solve the following problem in R.

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

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).


回答1:


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 


来源:https://stackoverflow.com/questions/31268826/least-square-optimization-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!