将函数从R转换为Matlab

时间:2017-04-07 12:19:18

标签: r matlab function equation

我试图在Matlab中重现R中的一个函数。我对R没有太多经验,所以我很难理解一些代码。这是R代码:

# Model Calculations
# ==================
#
# Function to determine length of stay df.
#
# t = time since hospital admission in days
# age = age at admision in years
# type = type of stroke (1. Haemorhagic, 2. Cerebral Infarction, 3. TIA)
#
los.df <- Vectorize(function(t, age, type,dest){
    par <- c(6.63570, -0.03652, -3.06931,  0.07153, -8.66118,
    0.08801, 22.10156, 2.48820, 1.56162, 1.27849,
    11.76860, 3.41989, 63.92514)
    alpha1 <- par[1]
    beta1 <- par[2]
    alpha2 <- par[3]
    beta2 <- par[4]
    theta0 <- par[5]
    theta1 <- par[6]
    mu1 <- par[7]
    mu2 <- par[8]
    mu3 <- par[9]
    mu4 <- 0
    nu1 <- 0
    nu2 <- 0
    nu3 <- par[10]
    nu4 <- 0
    rho1 <- 0
    rho2 <- par[11]
    rho3 <- par[12]
    rho4 <- par[13]
    #
    p <- exp(-exp(theta0 + theta1*age))
    temp.mat <- matrix(c(1,0,1,0,1-p,p),3,2,byrow=TRUE)
    initial.state.vec <- rep(0,7)
    initial.state.vec[c(type,type+1)] <- temp.mat[type,]
    #
    lambda1 <- exp(alpha1 + beta1*age)
    lambda2 <- exp(alpha2 + beta2*age)
    Q <- matrix(0,7,7)
    Q[1,] <- c(-(lambda1+mu1+nu1+rho1), lambda1, 0, 0, mu1, nu1, rho1)
    Q[2,] <- c(0, -(lambda2+mu2+nu2+rho2), lambda2, 0, mu2, nu2, rho2)
    Q[3,] <- c(0, 0, -(mu3+nu3+rho3), 0, mu3, nu3, rho3)
    Q[4,] <- c(0, 0, 0, -(mu4+nu4+rho4), mu4, nu4, rho4)
    Pt <- expm(t/365*Q)
    Ft <- sum(as.numeric(initial.state.vec %*% Pt)[dest:dest])
    return(Ft)
})

我很难理解以下代码行及其含义:     temp.mat <- matrix(c(1,0,1,0,1-p,p),3,2,byrow=TRUE) initial.state.vec <- rep(0,7) initial.state.vec[c(type,type+1)] <- temp.mat[type,]

这是我的Matlab代码,我试图重现R代码:

function Ft = losdf(age, strokeType, dest)

% function to determine length of stay in hospitaal of stroke patients
% t = time since admission (days);
% age = age of patient;
% strokeType = 1. Haemorhagic, 2. Cerebral Infarction, 3. TIA;
% dest = 5.Death 6.Nursing Home 7. Usual Residence;

alpha1 = 6.63570;
beta1 = -0.03652;
alpha2 = -3.06931;
beta2 = 0.07153;
theta0 = -8.66118; 
theta1 = 0.08801;
mu1 = 22.10156;
mu2 = 2.48820;
mu3 = 1.56162;
mu4 = 0;
nu1 = 0;
nu2 = 0;
nu3 = 1.27849;
nu4 = 0;
rho1 = 0;
rho2 = 11.76860;
rho3 = 3.41989;
rho4 = 63.92514;

Ft = zeros(365,1);
for t = 1:1:365
p = (exp(-exp(theta0 + (theta1.*age))));

if  strokeType == 1
    initialstatevec = [1 0 0 0 0 0 0];
elseif strokeType == 2
    initialstatevec = [0 1 0 0 0 0 0];
else
    initialstatevec = [0 0 (1-p) p 0 0 0];
end

lambda1 = exp(alpha1 + (beta1.*age));
lambda2 = exp(alpha2 + (beta2.*age));

Q = [ -(lambda1+mu1+nu1+rho1) lambda1  0  0  mu1  nu1  rho1; 
0  -(lambda2+mu2+nu2+rho2) lambda2 0 mu2 nu2 rho2; 
0 0 -(mu3+nu3+rho3) 0 mu3 nu3 rho3; 
0 0 0 -(mu4+nu4+rho4) mu4 nu4 rho4; 
0 0 0 0 0 0 0; 
0 0 0 0 0 0 0; 
0 0 0 0 0 0 0];

Pt = expm(t./365.*Q);
Pt = Pt(strokeType, dest);
Ft(t) = sum(initialstatevec.*Pt);

end

当我绘制输出时,它没有在Matlab中给出与在R中相同的值。我无法识别出错的地方;我知道我的Pt值是正确的。我想我在设置initialstatevec或定义Ft(t)时可能会出错?

如果有人能就我出错的地方给出建议,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

这一行temp.mat <- matrix(c(1,0,1,0,1-p,p),3,2,byrow=TRUE)创建一个包含3行和2列的矩阵,并通过按行填充来放置您给他的元素。

我们说p = 0.5temp.mat是这样的:

     [,1] [,2]
[1,]  1.0  0.0
[2,]  1.0  0.0
[3,]  0.5  0.5

这一行initial.state.vec <- rep(0,7)创建了一个七0的向量:

> initial.state.vec
[1] 0 0 0 0 0 0 0

此行initial.state.vec[c(type,type+1)] <- temp.mat[type,]根据type的值:1,2或3,将矩阵的type索引的行放在向量的特定索引中根据{{​​1}}的值计算。这意味着:

对于type,您获取矩阵的第一行,然后输入向量的第一和第二个索引。对于type = 1,您将获取矩阵的第二行,并将其放在索引2和3中,而对于type = 2,您将获取第三行,然后输入索引3和4。

type = 3的示例:

type = 3

希望它对你的翻译有所帮助。