我想最大化funcToOpt
代码中的。
数据说明:
wb
X1
是 T x N 矩阵,以及一个Nt
长度为 N 的向量- 每行之和
wb
为1 X1
横截面标准化(平均值=1 sd=0)
标准化意味着 的横截面平均值theta*X1
为零,这意味着它rowSums(wb + (theta1*X1) / Nt)
始终为 1。
这发生在我使用没有 NAs 值的 X1 矩阵时,是否有人知道我如何使行总和wi
等于1 当使用带有 NA 的矩阵时?
最大化代码:
X1 <- t(scale(t((X1)))) # X1 STANDARDIZATION
N <- dim(X1)[2]
T <- dim(X1)[1]
M <- wb + r + X1 # Nt, counting the NAs values
Nt <- rowSums(!is.na(M))
funcToOpt <- function (theta1)
{
inner <- rowSums((wb + (theta1*X1) / Nt) * r,na.rm = TRUE)
return (sum(inner, na.rm=TRUE) / T)
}
max<-optimize(funcToOpt, c(-50, 50), maximum = TRUE)
theta1=max[[1]]
wi=wb+(theta1*X1) / Nt
rowSums(wi,na.rm=T)
矩阵 5X5 无 NA
r=matrix(c(0.1,0.3,0.4,0.3,0.4,0.5,0.3,0.3,0.2,0.1,0.1,0.1,0.1,0.1,0.2,0.9,0.3,0.2,0.1,0.2,0.2,0.4,0.4,0.5,0.1),ncol = 5)
wb=matrix(c(0.3,0.3,-0.4,-0.3,0.4,-0.5,-0.3,0.3,0.2,0.1,0.1,0.1,0.1,0.1,0.2,0.9,0.3,0.6,0.5,0.2,0.2,0.6,0.4,0.5,0.1),ncol = 5)
X1=matrix(c(1:20,0,4,5,0,3),ncol = 5)
MATRIX 5x5 不适用
r=matrix(c(0.1,0.3,0.4,0.3,0.4,0.5,0.3,0.3,0.2,0.1,0.1,0.1,0.1,0.1,0.2,0.9,0.3,0.2,0.1,0.2,0.2,0.4,0.4,0.5,0.1),ncol = 5)
wb=matrix(c(0.3,0.3,-0.4,-0.3,0.4,-0.5,-0.3,0.3,0.2,0.1,0.1,0.1,0.1,0.1,0.2,0.9,0.3,0.6,0.5,0.2,0.2,0.6,0.4,0.5,0.1),ncol = 5)
X1=matrix(c(1:20,NA,4,5,NA,3),ncol = 5)