我正在使用 R 編程語言。
我有以下資料:
1) 均值向量(4 行 1 列)
4 個變數(x1、x2、x3、x4)
5.0060022
3.4280049
1.4620007
0.2459998
2)協方差矩陣 (4行4列)
4 個變數(對角元素是 x1、x2、x3、x4,成對元素是例如第二個元素:(x1,x2)、第三個元素 (x1,x3)、第四個元素 (x1, x4) 等)
0.15065114 0.13080115 0.02084463 0.01309107
0.13080115 0.17604529 0.01603245 0.01221458
0.02084463 0.01603245 0.02808260 0.00601568
0.01309107 0.01221458 0.00601568 0.01042365
問題:我想獲取上述資料并以以下格式創建一個函式(具有 4 個輸入:x1、x2、x3、x4 和單個數字作為輸出):

這是我到目前為止嘗試過的:
my_function <- function(x_one, x_two, x_three, x_four)
{
sigma1.pre <- c(0.15065114 , 0.13080115 , 0.02084463 , 0.01309107 , 0.13080115 , 0.17604529 , 0.01603245 , 0.01221458 , 0.02084463 , 0.01603245 , 0.02808260 , 0.00601568 , 0.01309107 , 0.01221458 , 0.00601568 , 0.01042365)
sigma1 <- matrix(sigma1.pre, nrow=4, ncol= 4, byrow = TRUE)
sigma1_inv <- ginv(sigma1)
det_sigma1_inv <- det(sigma1_inv)
denom = sqrt( (2*pi)^4 * det_sigma1_inv)
x_one = x1 - 5
x_two = x2 - 3.42
x_three = x3 - 1.462
x_four = x4 - 0.245
x_t = c(x_one, x_two, x_three, x_four)
x_t_one <- matrix(x_t, nrow=4, ncol= 1, byrow = TRUE)
x_t_one_t = -0.5 * t(x_t_one)
x_t_two = matrix(x_t, nrow=1, ncol= 4, byrow = TRUE)
num = exp(x_t_two %*% sigma1_inv %*% x_t_one_t)
answer = num/denom
return(answer)
}
問題:當我嘗試運行此函式時:
my_function(1,2,3,4)
我收到以下錯誤:
Error in x_t_two %*% sigma1_inv %*% x_t_one_t : non-conformable arguments
我認為錯誤是由于矩陣乘法而發生的
num = exp(x_t_two %*% sigma1_inv %*% x_t_one_t)
我試圖改變矩陣乘法的順序:
num = exp( x_t_one_t %*% sigma1_inv %*% x_t_two )
但是錯誤仍然存??在。
有人可以告訴我如何解決這個問題嗎?
謝謝!
參考:
- https://stat.ethz.ch/R-manual/R-devel/library/base/html/matmult.html
- https://en.wikipedia.org/wiki/Multivariate_normal_distribution
- https://wikimedia.org/api/rest_v1/media/math/render/svg/c66e6f6abd66698181e114a4b00da97446efd3c4
uj5u.com熱心網友回復:
正如我上面提到的,dmvnorm函式回傳您顯示的函式的值。
dmvnorm(c(5,3,1,0),m,v)
[1] 0.01074766
這是我的手動版本,
func <- function(vec, m, v){
if (length(vec) != length(m)) {
stop("dimension error")
} # and several more
a <- t(vec - m) %*% solve(v) %*% (vec - m)
k <- length(vec)
return(exp(-a/2)/sqrt((2*pi)^k * det(v)))
}
func(c(5,3,1,0) , m, v)
[,1]
[1,] 0.01074766
在您的函式中,您的函式不起作用的主要原因是 in line num = exp(x_t_two %*% sigma1_inv %*% x_t_one_t),維度x_t_one_t是錯誤的。當您將其設定為 時nrow = 4, ncol = 1,它已經是4*1,您無需將其轉置。我對您的功能添加了更多評論。
my_function <- function(x_one, x_two, x_three, x_four)
{
sigma1.pre <- c(0.15065114 , 0.13080115 , 0.02084463 , 0.01309107 , 0.13080115 , 0.17604529 , 0.01603245 , 0.01221458 , 0.02084463 , 0.01603245 , 0.02808260 , 0.00601568 , 0.01309107 , 0.01221458 , 0.00601568 , 0.01042365)
sigma1 <- matrix(sigma1.pre, nrow=4, ncol= 4, byrow = TRUE)
# You can also use solve instead of ginv, solve is in base R
sigma1_inv <- ginv(sigma1)
det_sigma1_inv <- det(sigma1_inv)
# In here, not det_sigma1_inv, just use det(sigma1) will work.
denom = sqrt( (2*pi)^4 * det(sigma1))
#in below part, I recommend another way.
#m <- c( 5.0060022, 3.4280049, 1.4620007, 0.2459998)
#x_t = c(x_one, x_two, x_three, x_four)
#There was no input x1, x2, x3, x4
x_one = x_one - 5.0060022
x_two = x_two - 3.4280049
x_three = x_three - 1.4620007
x_four = x_four - 0.2459998
# Vectors and matrices are handle as vector and matrices. You do not need to
#change vectors to matrices.
#x_t_t = x_t - m
x_t = c(x_one, x_two, x_three, x_four)
x_t_one <- matrix(x_t, nrow=4, ncol= 1, byrow = TRUE)
x_t_two = matrix(x_t, nrow=1, ncol= 4, byrow = TRUE)
# In this part, as it's (x-mu)^T * SIGMA * (x-mu), dimension of x_t_one_t was wrong
# You may try another way.
#num = exp(-0.5 * t(x_t_t) %*% sigma1_inv %*% x_t_t)
num = exp(-0.5 * x_t_two %*% sigma1_inv %*% x_t)
answer = num/denom
return(answer)
}
my_function(5,3,1,0)
[,1]
[1,] 0.01074766
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/330887.html
