<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Converting R code to SAS IML in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Converting-R-code-to-SAS-IML/m-p/351177#M3427</link>
    <description>&lt;PRE&gt;
I noticed in R , object function is 
   -sum(-r*phi*x+(n-r)*log(1-exp(-phi*x)))
in IML, there is not minus .
is it right ?


&lt;/PRE&gt;</description>
    <pubDate>Wed, 19 Apr 2017 07:52:17 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2017-04-19T07:52:17Z</dc:date>
    <item>
      <title>Converting R code to SAS IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Converting-R-code-to-SAS-IML/m-p/351104#M3426</link>
      <description>&lt;P&gt;I am trying to covert the following R code to SAS IML&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;LDA&amp;lt;-function(I,r,x,n) {&lt;BR /&gt;&amp;nbsp; total&amp;lt;-sum(r*x)&lt;BR /&gt;&amp;nbsp; options(warn=-1)&lt;BR /&gt;&amp;nbsp; lik&amp;lt;-function(phi) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; -sum(-r*phi*x+(n-r)*log(1-exp(-phi*x)))&lt;BR /&gt;&amp;nbsp; }&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp; #Maximizing phi&lt;BR /&gt;&amp;nbsp; actual=optim(0.5,lik)$par&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp; if ((1/actual)&amp;gt;x[I]) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; u=cat(as.integer(1/actual)," ",paste("Warning:Estimate exceeds maximum cells tested per well"),"\n")&lt;BR /&gt;&amp;nbsp; } else {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; u=(as.integer(1/actual))&lt;BR /&gt;&amp;nbsp; }&lt;BR /&gt;&amp;nbsp; print(paste("Estimated from LDA"))&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp; return(c(u,total))&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For the following test data&lt;/P&gt;&lt;P&gt;I&amp;lt;-9&lt;BR /&gt;r&amp;lt;-c(0, 0, 0, 6, 8, 8, 8, 8, &lt;span class="lia-unicode-emoji" title=":smiling_face_with_sunglasses:"&gt;😎&lt;/span&gt;&lt;BR /&gt;x&amp;lt;-c(50000, 10000, 2000, 400, 80, 16, 3.2, 0.64, 0.128)&lt;BR /&gt;n&amp;lt;-c(8, 8, 8, 8, 8, 8, 8, 8, &lt;span class="lia-unicode-emoji" title=":smiling_face_with_sunglasses:"&gt;😎&lt;/span&gt;&lt;BR /&gt;LDA(I,r,x,n)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The result is 871 and 3199.744.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my IML code.&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;iml&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#008000" face="Courier New" size="2"&gt;I=9;&lt;BR /&gt;r={0, 0, 0, 6, 8, 8, 8, 8, 8};&lt;BR /&gt;x={50000, 10000, 2000, 400, 80, 16, 3.2, 0.64, 0.128};&lt;BR /&gt;n={8, 8, 8, 8, 8, 8, 8, 8, 8};&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;total=sum(r#x);&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;print&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; I, r, x, n, total;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;start&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; lik(phi) global(r, x, n);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;f=sum(-r#phi#x+(n-r)#log(&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;-exp(-phi#x)));&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;return&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;(f);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;finish&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; lik;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;phi={&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;0.5&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;};&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;optn={&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;, &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;4&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;, &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;10&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;};&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;con={&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;0.0000000001&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;, &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;};&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;parm={&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;,&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;0.1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;};&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;call&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; nlpnms(rc, result, &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="2"&gt;"lik"&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;, phi, optn, con, , parm);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;u=round(&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;/result,&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;if&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; u&amp;gt;x[&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;] &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;then&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; print &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="2"&gt;"Warning: Estimate exceeds maximum cells tested per well"&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;print&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; u, total;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;quit&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The result I got is 876 and 3199.744.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What do I need to do in&amp;nbsp;the IML code in order&amp;nbsp;to get the same result as R code?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;JJ&lt;/P&gt;</description>
      <pubDate>Wed, 19 Apr 2017 02:08:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Converting-R-code-to-SAS-IML/m-p/351104#M3426</guid>
      <dc:creator>JJLee</dc:creator>
      <dc:date>2017-04-19T02:08:32Z</dc:date>
    </item>
    <item>
      <title>Re: Converting R code to SAS IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Converting-R-code-to-SAS-IML/m-p/351177#M3427</link>
      <description>&lt;PRE&gt;
I noticed in R , object function is 
   -sum(-r*phi*x+(n-r)*log(1-exp(-phi*x)))
in IML, there is not minus .
is it right ?


&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Apr 2017 07:52:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Converting-R-code-to-SAS-IML/m-p/351177#M3427</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-04-19T07:52:17Z</dc:date>
    </item>
    <item>
      <title>Re: Converting R code to SAS IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Converting-R-code-to-SAS-IML/m-p/351189#M3428</link>
      <description>&lt;PRE&gt;
I still get 876 after running Genetic Algorithm.



proc iml;


r={0, 0, 0, 6, 8, 8, 8, 8, 8};
xx={50000, 10000, 2000, 400, 80, 16, 3.2, 0.64, 0.128};
n={8, 8, 8, 8, 8, 8, 8, 8, 8};
total=sum(r#xx);

start func(x) global(r, xx, n);
f=sum(-r#x#xx+(n-r)#log(1-exp(-x#xx)));
return(f);
finish;

id=gasetup(1,1,1234);
call gasetobj(id,1,"func");
call gasetsel(id,100,1,0.95);
call gasetcro(id,0.05,4);
call gasetmut(id,0.05);

call gainit(id,10000,{1e-6,10});
niter = 1000;
summary = j(niter,2);
mattrib summary [c = {"Min Value", "Avg Value"} l=""];
do i = 1 to niter;
 call garegen(id);
 call gagetval(value, id);
 summary[i,1] = value[1];
 summary[i,2] = value[:];
end;
call gagetmem(mem, value, id, 1);
print mem[ l="best member:"],
      "Max Value: " value[l = ""] ;
u=round(1/mem,1);
print u,total;
call gaend(id);
quit;





best member:
0.0011416
Max Value:	-6.520232
u
876
total
3199.744



Maybe there is some little different in object function between R and SAS.

Or you could check which obj has a bigger value.

proc iml;
r={0, 0, 0, 6, 8, 8, 8, 8, 8};
x={50000, 10000, 2000, 400, 80, 16, 3.2, 0.64, 0.128};
n={8, 8, 8, 8, 8, 8, 8, 8, 8};

start lik(phi) global(r, x, n);
f=sum(-r#phi#x+(n-r)#log(1-exp(-phi#x)));
return(f);
finish ;
phi={0.5};
optn={1, 2};
con={1e-6, .};

R=lik(1/871);
SAS=lik(1/876);

print R,SAS;
quit;


r
-6.520349
SAS
40.793882
&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Apr 2017 08:23:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Converting-R-code-to-SAS-IML/m-p/351189#M3428</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-04-19T08:23:00Z</dc:date>
    </item>
    <item>
      <title>Re: Converting R code to SAS IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Converting-R-code-to-SAS-IML/m-p/351247#M3429</link>
      <description>&lt;P&gt;When you perform numerical optimization in statistical software, the algorithms have various "convergence criteria" which tells the software when to stop iterating and report an answer. The criteria differs from algorithm&amp;nbsp;to algorithm and between different software. For this problem, both software are finding the "same" solution, but the solutions differ by about 5E-6, which is a typical range for numerical optimization.&amp;nbsp;Because you are reporting 1/ result, this small difference gets magnified.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In your R program, you can see the actual solution (and/or the reciprocal value) by using&lt;/P&gt;
&lt;P&gt;actual=optim(0.5,lik)$par&lt;BR /&gt;sprintf("%20.16f",actual)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; [1] " &lt;STRONG&gt;0.00114&lt;/STRONG&gt;74609374996"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the IML program, you can use&amp;nbsp;&lt;/P&gt;
&lt;P&gt;print result[f=20.16];&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;STRONG&gt;0.00114&lt;/STRONG&gt;17389904598 &lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;To resolving the issue might be tough. You need to understand the convergence criteria for both software. The SAS/IML &lt;A href="http://support.sas.com/documentation/cdl/en/imlug/68150/HTML/default/viewer.htm#imlug_nonlinearoptexpls_sect019.htm" target="_self"&gt;documentation includes complete details of the termination criteria and how to control the algorithms.&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Personally, I would not pursue trying to resolve the difference. The problem is not in the software. The problem is that you are reporting an ill-conditioned quantity (the reciprocal of a small value) which is highly sensitive to the numerical optimization. A better choice is to report the optimal value itself, not the reciprocal.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Apr 2017 12:18:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Converting-R-code-to-SAS-IML/m-p/351247#M3429</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2017-04-19T12:18:22Z</dc:date>
    </item>
    <item>
      <title>Re: Converting R code to SAS IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Converting-R-code-to-SAS-IML/m-p/351474#M3430</link>
      <description>&lt;P&gt;Many thanks for everyone!&lt;/P&gt;</description>
      <pubDate>Wed, 19 Apr 2017 22:18:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Converting-R-code-to-SAS-IML/m-p/351474#M3430</guid>
      <dc:creator>JJLee</dc:creator>
      <dc:date>2017-04-19T22:18:36Z</dc:date>
    </item>
  </channel>
</rss>

