将函数从 R 翻译成 Matlab
Translating a function from R into Matlab
我正在尝试在 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,]
这是我尝试重现 R 代码的 Matlab 代码:
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) 时出错了?
如果有人能就我哪里出错的地方给我建议,我将不胜感激。
这一行 temp.mat <- matrix(c(1,0,1,0,1-p,p),3,2,byrow=TRUE)
创建了一个 3 行 2 列的矩阵,并通过逐行填充来放置你给他的元素。
假设 p = 0.5
和 temp.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
索引的行放入向量的特定索引中也是按照type
的值计算的。这意味着:
对于 type = 1
,您取矩阵的第一行,然后放入向量的第一个和第二个索引。对于 type = 2
,您取矩阵的第 2 行并将其放入索引 2 和 3,对于 type = 3
,您取第 3 行并将其放入索引 3 和 4。
type = 3
示例:
temp.mat[type,]
[1] 0.5 0.5
initial.state.vec
[1] 0.0 0.0 0.5 0.5 0.0 0.0 0.0
希望对您的翻译有所帮助。
我正在尝试在 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,]
这是我尝试重现 R 代码的 Matlab 代码:
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) 时出错了?
如果有人能就我哪里出错的地方给我建议,我将不胜感激。
这一行 temp.mat <- matrix(c(1,0,1,0,1-p,p),3,2,byrow=TRUE)
创建了一个 3 行 2 列的矩阵,并通过逐行填充来放置你给他的元素。
假设 p = 0.5
和 temp.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
索引的行放入向量的特定索引中也是按照type
的值计算的。这意味着:
对于 type = 1
,您取矩阵的第一行,然后放入向量的第一个和第二个索引。对于 type = 2
,您取矩阵的第 2 行并将其放入索引 2 和 3,对于 type = 3
,您取第 3 行并将其放入索引 3 和 4。
type = 3
示例:
temp.mat[type,]
[1] 0.5 0.5
initial.state.vec
[1] 0.0 0.0 0.5 0.5 0.0 0.0 0.0
希望对您的翻译有所帮助。