简体   繁体   中英

Least square optimization in R

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 mxn ). Eg:

    > 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 (ie minimizes the sum of squares).

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM