Hi All, Am trying to replicate a minimization from R using SAS IML. Below are sample data and IML code. The idea is to find a value for alpha that minimizes mean(gx * id)/(mean(id)**2) using the Nelder-Mead method. I should begin by saying that I’m not too familiar with SAS IML. I’ve worked my way through a number of snags but am stuck again. Some or all of my current troubles may lie with the “call nlpnms” part of the code. I’m certainly struggling with that. It may be that there are still problems occurring earlier in the code though. data propensity;
input PTNO TRT PROP1 PROP2 PROP3;
gx = sum(1/prop1, 1/prop2, 1/prop3);
id = .;
cards;
3 1 0.64347 0.34041 0.01612
8 1 0.59957 0.36649 0.03393
9 2 0.58017 0.37490 0.04493
5 2 0.57192 0.37787 0.05021
1 1 0.55445 0.38299 0.06256
2 1 0.20073 0.24442 0.55485
6 3 0.23172 0.26988 0.49840
7 2 0.26402 0.29412 0.44186
4 3 0.35394 0.34892 0.29714
;
run;
proc iml;
use propensity;
read all var {gx};
close propensity;
start f_min(alpha) global(gx);
id = gx < alpha;
fn = mean(gx` * id)/(mean(id)**2);
return(fn);
finish f_min;
alpha = {20.3996646}; * median of gx;
optn = {0};
call nlpnms(rc, xr, "f_min", alpha, optn);
quit; Here are the results from the calculation done in R. These suggest the value for alpha that minimizes mean(gx * id)/(mean(id)**2) should be about 24.47922. > require(nnet) > > Y <- c(102, 105, 120, 130, 100, 80, 94, 108, 96) > W <- c(1, 1, 1, 3, 2, 3, 2, 1, 2) > X <- c(5.5, 10.6, 3.1, 8.7, 5.1, 10.2, 9.8, 4.4, 4.9) > > W <- relevel(as.factor(W), ref = 1) > PF.out <- multinom(W ~ X) # weights: 9 (4 variable) initial value 9.887511 iter 10 value 8.236707 final value 8.235926 converged > PF.fit <- fitted(PF.out) > > fn <- function (alpha) + { + gx <- apply(1/PF.fit, 1, sum) + id <- (gx < alpha) + mean(gx * id)/(mean(id)^2) + } > > optim(20.39935, fn, method = "Nelder-Mead", lower = -Inf, upper = Inf) $par [1] 24.47922 $value [1] 21.02992 $counts function gradient 6 NA $convergence [1] 0 $message NULL Thanks, Paul
... View more