SAS中的PROC模型写在R中

时间:2015-06-08 12:05:46

标签: r

我有一个SAS代码如下:

proc model;
  parms 
     X1 X2 X3 a2 a3 a4
;
  S = prfl1*X1 + prfl2*X2 + prfl3*X3 
;
      Z0= S-a2;
      R = Z0 + (Z0**2 + (2*a4*S)/(100**a3))**0.5;
      estH100 = S * (T**a3*(100**a3*R + a4))/(100**a3*(T**a3*R + a4));

      FIT est100
    START = (
   X1=20 X2=20 X3=20  a2=34.700396595 a3=1.7086613571 a4=8901.6700482
    )
        / OLS DATA=a startiter maxiter=10000 converge = 0.000001
          out=pns outresid outpredict outactual outest=pnsest4;

RUN;

我在 R

中编写了同样的程序
nls(estH100 = (prfl1*X1 + prfl2*X2 + prfl3*X3) * (T**a3*(100**a3*
(((prfl1*X1 + prfl2*X2 + prfl3*X3)-a2) + (((prfl1*X1 + prfl2*X2 + prfl3*X3)-
a2)**2 + (2*a4*(prfl1*X1 + prfl2*X2 + prfl3*X3))/(100**a3))**0.5) + 
a4))/(100**a3*(T**a3*(((prfl1*X1 + prfl2*X2 + prfl3*X3)-a2) + (((prfl1*X1 + 
prfl2*X2 + prfl3*X3)-a2)**2 + (2*a4*(prfl1*X1 + prfl2*X2 + prfl3*X3))/(100**a3))**0.5) + a4))
, data = a, start = (a2=0.1, a3=2, a4=4, X1=20, X2 = 20, X3 = 20 ))

它有效。但是,我不喜欢R代码中的结构。每当它们出现在函数中时,我必须替换S,Z0和R.是否有可能以与SAS ????

类似的方式编写这些内容

1 个答案:

答案 0 :(得分:1)

1)功能定义一个函数并将计算结果放入其中:

f <- function(a2, a3, a4, X1, X2, X3, prf11, prf12, prf13, T) {
    S <- prfl1*X1 + prfl2*X2 + prfl3*X3
    Z0 <- S-a2
    R <- Z0 + (Z0^2 + (2*a4*S)/(T0^a3))^0.5
    S * (T^a3*(100^a3*R + a4))/(100^a3*(T^a3*R + a4))
}
nls(estH100 ~ f(a2, a3, a4, X1, X2, X3, prf11, prf12, prf13, T),
  data = a, start = list(a2 = 0.1, a3 = 2, a4 = 4, X1 = 20, X2 = 20, X3 = 20 ))

2)替代另一种方法是使用substitute,如下所示:

S <- quote(prfl1*X1 + prfl2*X2 + prfl3*X3)
Z0 <- substitute(S-a2, list(S = S))
R <- substitute(Z0 + (Z0^2 + (2*a4*S)/(T0^a3))^0.5, list(Z0 = Z0))
fo <- substitute(estH100 ~ S * (T^a3*(100^a3*R + a4))/(100^a3*(T^a3*R + a4)), 
 list(S = S, R = R))

此时fo如下所示,可以在nls中使用:

> fo
estH100 ~ (prfl1 * X1 + prfl2 * X2 + prfl3 * X3) * (T^a3 * (100^a3 * 
    (prfl1 * X1 + prfl2 * X2 + prfl3 * X3 - a2 + ((prfl1 * X1 + 
        prfl2 * X2 + prfl3 * X3 - a2)^2 + (2 * a4 * S)/(T0^a3))^0.5) + 
    a4))/(100^a3 * (T^a3 * (prfl1 * X1 + prfl2 * X2 + prfl3 * 
    X3 - a2 + ((prfl1 * X1 + prfl2 * X2 + prfl3 * X3 - a2)^2 + 
    (2 * a4 * S)/(T0^a3))^0.5) + a4))


> nls(fo, data = a, start = list(a2 = 0.1, a3 = 2, a4 = 4, X1 = 20, X2 = 20, X3 = 20 ))