<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Victor H. Aguiar  -Economics Researcher-</title>
	<atom:link href="http://vhaguiar.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://vhaguiar.wordpress.com</link>
	<description>Stata, R, Python, Statistics, Mathematics and Economics</description>
	<lastBuildDate>Thu, 12 Jan 2012 15:35:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='vhaguiar.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Victor H. Aguiar  -Economics Researcher-</title>
		<link>http://vhaguiar.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://vhaguiar.wordpress.com/osd.xml" title="Victor H. Aguiar  -Economics Researcher-" />
	<atom:link rel='hub' href='http://vhaguiar.wordpress.com/?pushpress=hub'/>
		<item>
		<title>R: maximum entropy regression linear and non linear</title>
		<link>http://vhaguiar.wordpress.com/2011/12/29/r-maximum-entropy-regression-linear-and-non-linear-2/</link>
		<comments>http://vhaguiar.wordpress.com/2011/12/29/r-maximum-entropy-regression-linear-and-non-linear-2/#comments</comments>
		<pubDate>Thu, 29 Dec 2011 00:03:06 +0000</pubDate>
		<dc:creator>vhaguiar</dc:creator>
				<category><![CDATA[R]]></category>

		<guid isPermaLink="false">https://vhaguiar.wordpress.com/?p=135</guid>
		<description><![CDATA[This code is for implementation of maximum entropy&#160; non linear and linear estimation of regression in R. You will need Alabama package. ## PARA REGRESIONES LINEALES. ## Funcion GENERALIZED MAXIMUM ENTROPY gme.v1=function(Y=Y,X=X,M=M,C=C,opt1=opt1,opt2=opt2,matprior=matprior,matprior2=matprior2) {&#160; ## Support Matrix of dim k x M. if (opt2==&#8221;noprior&#8221;) {&#160;&#160;&#160; &#160;&#160;&#160; z1=rep(0,M)&#160;&#160;&#160; nz1=floor(length(z1)/2)&#160;&#160;&#160; dum&#60;-1/nz1&#160;&#160;&#160; dum&#60;-seq(0,1,by=(dum))*C&#160;&#160;&#160; z1&#60;-c(-rev(dum[2:(nz1+1)]),dum)&#160;&#160;&#160; Z=kronecker(diag(1,dim(X)[2]), t(z1))&#160;&#160;&#160; ## inicial po.&#160;&#160;&#160; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vhaguiar.wordpress.com&amp;blog=7156458&amp;post=135&amp;subd=vhaguiar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This code is for implementation of maximum entropy&nbsp; non linear and linear estimation of regression in R. You will need Alabama package. </p>
<p>## PARA REGRESIONES LINEALES. <br />## Funcion GENERALIZED MAXIMUM ENTROPY </p>
<p>gme.v1=function(Y=Y,X=X,M=M,C=C,opt1=opt1,opt2=opt2,matprior=matprior,matprior2=matprior2) {<br />&nbsp; ## Support Matrix of dim k x M.</p>
<p>if (opt2==&#8221;noprior&#8221;) {&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; z1=rep(0,M)<br />&nbsp;&nbsp;&nbsp; nz1=floor(length(z1)/2)<br />&nbsp;&nbsp;&nbsp; dum&lt;-1/nz1<br />&nbsp;&nbsp;&nbsp; dum&lt;-seq(0,1,by=(dum))*C<br />&nbsp;&nbsp;&nbsp; z1&lt;-c(-rev(dum[2:(nz1+1)]),dum)<br />&nbsp;&nbsp;&nbsp; Z=kronecker(diag(1,dim(X)[2]), t(z1))<br />&nbsp;&nbsp;&nbsp; ## inicial po.<br />&nbsp;&nbsp;&nbsp; p1o=rep(1,length(z1))<br />&nbsp;&nbsp;&nbsp; p1o=p1o/sum(p1o)<br />&nbsp;&nbsp;&nbsp; po&lt;-rep(p1o,dim(X)[2])</p>
<p>}</p>
<p>if (opt2==&#8221;prior&#8221;) {<br />&nbsp;&nbsp;&nbsp; z1=rep(0,M)<br />&nbsp;&nbsp;&nbsp; Z=kronecker(diag(1,dim(X)[2]), t(z1))<br />&nbsp;&nbsp;&nbsp; ## rownames<br />&nbsp;&nbsp;&nbsp; dum1&lt;-rep(&#8220;beta&#8221;,dim(X)[2])<br />&nbsp;&nbsp;&nbsp; dum2&lt;-seq(1:dim(X)[2])<br />&nbsp;&nbsp;&nbsp; dum1&lt;-paste(dum1,dum2,sep=&#8221;")<br />&nbsp;&nbsp;&nbsp; rownames(Z)&lt;-dum1<br />&nbsp;&nbsp;&nbsp; ## colnames<br />&nbsp;&nbsp;&nbsp; dum1&lt;-rep(&#8220;p&#8221;,M)<br />&nbsp;&nbsp;&nbsp; dum2&lt;-seq(1:M)<br />&nbsp;&nbsp;&nbsp; dum1&lt;-paste(dum1,dum2,sep=&#8221;")<br />&nbsp;&nbsp;&nbsp; dum2&lt;-rep(dum1,dim(X)[2])<br />&nbsp;&nbsp;&nbsp; colnames(Z)&lt;-dum2<br />&nbsp;&nbsp;&nbsp; ## <br />&nbsp;&nbsp;&nbsp; dum=0<br />&nbsp;&nbsp;&nbsp; for (i in 1:dim(X)[2]) {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dum1=1+dum<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Z[i,dum1:(dum1+(M-1))]&lt;-matprior[i,]<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dum=dum+(M)<br />&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; ## inicial po.<br />&nbsp;&nbsp;&nbsp; p1o=rep(0,length(z1))<br />&nbsp;&nbsp;&nbsp; po&lt;-rep(p1o,dim(X)[2])<br />&nbsp;&nbsp;&nbsp; dum=0<br />&nbsp;&nbsp;&nbsp; for (i in 1:dim(X)[2]) {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dum1=1+dum<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; po[dum1:(dum1+(M-1))]&lt;-matprior2[i,]<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dum=dum+M<br />&nbsp;&nbsp;&nbsp; }</p>
<p>}<br />&nbsp;&nbsp;&nbsp; <br />## Soportes para w, en este caso se va a utilizar la <br />## regla 3sigma. <br />## sigma es la desviaci?n est?ndar emp?rica de Y.<br />sigma=sd(Y)<br />## El soporte para J=5. <br />## El valor m?nimo debe ser -3*sigma<br />## El valor m?ximo debe ser 3*sigma<br />## Deben ser sim?tros alrrededor de cero.<br />## vt=(-3*sigma,-1.5*sigma,0,1.5*sigma,3*sigma)<br />v1&lt;-c(-3*sigma,-1.5*sigma,0,1.5*sigma,3*sigma)</p>
<p>V=kronecker(diag(1,length(Y)), t(v1))</p>
<p>## inicial po.<br />w1o=rep(1,length(v1))<br />w1o=w1o/sum(w1o)<br />wo&lt;-rep(w1o,length(Y))</p>
<p>## Valor inicial total.<br />pwo&lt;-c(po,wo)</p>
<p>## k <br />k=length(po)</p>
<p>## Equality restrictions.<br />## h[i]=0<br />heq&lt;-function(pw=pw,Z=Z,V=V,k=k,Y=Y,X=X) {<br />## pw vector de par?metros que concatena a p y w.<br />## Z, es la matriz de soportes de los Beta.<br />## V, es la matriz de soportes de los errores.<br />## k, longitud del par?metro p. <br />&nbsp;&nbsp;&nbsp; len=length(pw)<br />&nbsp;&nbsp;&nbsp; p=pw[1:k]<br />&nbsp;&nbsp;&nbsp; w=pw[(k+1):len]<br />&nbsp;&nbsp;&nbsp; ## tama?o de restricciones.<br />&nbsp;&nbsp;&nbsp; res.1&lt;-dim(V)[2]<br />&nbsp;&nbsp;&nbsp; ## restricciones de probabilidades.<br />&nbsp;&nbsp;&nbsp; res.2&lt;-2<br />&nbsp;&nbsp;&nbsp; res&lt;-res.1+res.2<br />&nbsp;&nbsp;&nbsp; h&lt;-rep(NA,res)<br />&nbsp;&nbsp;&nbsp; ## Restricciones lineales.<br />&nbsp;&nbsp;&nbsp; h[1:res.1]&lt;-X%*%(Z%*%p)+(V%*%w)-Y<br />&nbsp;&nbsp;&nbsp; ## Restricciones de probabilidades.<br />&nbsp;&nbsp;&nbsp; h[(res.1+1)]&lt;-sum(p,na.rm=TRUE)-1<br />&nbsp;&nbsp;&nbsp; h[(res.1+2)]&lt;-sum(w,na.rm=TRUE)-1<br />&nbsp;&nbsp;&nbsp; h<br />}&nbsp;&nbsp;&nbsp; </p>
<p>## Inequality restrictions.<br />## h[i]&gt;0<br />hin&lt;-function(pw=pw,Z=Z,V=V,k=k,Y=Y,X=X) {<br />## pw vector de par?metros que concatena a p y w.<br />## Z, es la matriz de soportes de los Beta.<br />## V, es la matriz de soportes de los errores.<br />## k, longitud del par?metro p. <br />&nbsp;&nbsp;&nbsp; len=length(pw)<br />&nbsp;&nbsp;&nbsp; h&lt;-rep(NA,(2*len))<br />&nbsp;&nbsp;&nbsp; ## Restricciones lineales.<br />&nbsp;&nbsp;&nbsp; h[1:len]&lt;-pw<br />&nbsp;&nbsp;&nbsp; h[(len+1):(2*len)]&lt;- -pw+1<br />}&nbsp;&nbsp;&nbsp; </p>
<p>H.obj&lt;-function(pw=pw,Z=Z,V=V,k=k,Y=Y,X=X) {<br />## pw vector de par?metros que concatena a p y w.<br />## Z, es la matriz de soportes de los Beta.<br />## V, es la matriz de soportes de los errores.<br />## k, longitud del par?metro p. <br />&nbsp;&nbsp;&nbsp; len=length(pw)<br />&nbsp;&nbsp;&nbsp; p=pw[1:k]<br />&nbsp;&nbsp;&nbsp; w=pw[(k+1):len]<br />&nbsp;&nbsp;&nbsp; Objective=as.numeric(-(t(p)%*%log(p)))+as.numeric(-(t(w)%*%log(w)))<br />&nbsp;&nbsp;&nbsp; -Objective<br />}</p>
<p>## gradiente, recibe R^n y bota R^n.<br />H.gr&lt;-function(pw=pw,Z=Z,V=V,k=k,Y=Y,X=X) {<br />## pw vector de par?metros que concatena a p y w.<br />## Z, es la matriz de soportes de los Beta.<br />## V, es la matriz de soportes de los errores.<br />## k, longitud del par?metro p. <br />&nbsp;&nbsp;&nbsp; len=length(pw)<br />&nbsp;&nbsp;&nbsp; p=pw[1:k]<br />&nbsp;&nbsp;&nbsp; w=pw[(k+1):len]<br />&nbsp;&nbsp;&nbsp; Grad.p&lt;-log(p)+1<br />&nbsp;&nbsp;&nbsp; Grad.w&lt;-log(w)+1<br />&nbsp;&nbsp;&nbsp; Grad&lt;-c(Grad.p,Grad.w)<br />&nbsp;&nbsp;&nbsp; Grad<br />}<br />## El default de auglag y optim es minimizar.</p>
<p>################################################################<br />## Optimizaci?n.<br />################################################################<br />if (opt1==&#8221;nongr&#8221;) {<br />&nbsp;&nbsp;&nbsp; results&lt;-constrOptim.nl(par=pwo, fn=H.obj, heq=heq,hin=hin,Z=Z,V=V,k=k,Y=Y,X=X)<br />}</p>
<p>if (opt1==&#8221;gr&#8221;) {<br />&nbsp;&nbsp;&nbsp; ## con gradiente<br />&nbsp;&nbsp;&nbsp; results&lt;-constrOptim.nl(par=pwo, fn=H.obj, gr=H.gr, heq=heq,hin=hin,Z=Z,V=V,k=k,Y=Y,X=X)<br />}<br />##beta<br />## recobro los p del par=pw y multiplico por el soporte Z, para obtener el vector de coeficientes.<br />beta&lt;-Z%*%results$par[1:k]</p>
<p>e&lt;-V%*%results$par[(k+1):length(results$par)]</p>
<p>&nbsp;&nbsp;&nbsp; list(beta=beta,e=e)<br />}</p>
<p>## Generalized Maximum Entropy no lineal&nbsp; Especializada para </p>
<p>gme.nl=function(Y=Y,X=X,M=M,C=C,fun=fun,opt1=opt1,opt2=opt2,matprior=matprior,matprior2=matprior2) {<br />## inicializacion de I y L<br />&nbsp; I=X[,"I"]<br />&nbsp; L=X[,"L"]<br />&nbsp; <br />## Support Matrix of dim k x M.</p>
<p>if (opt2==&#8221;noprior&#8221;) {&nbsp; <br />&nbsp;&nbsp;&nbsp; z1=rep(0,M)<br />&nbsp;&nbsp;&nbsp; nz1=floor(length(z1)/2)<br />&nbsp;&nbsp;&nbsp; dum&lt;-1/nz1<br />&nbsp;&nbsp;&nbsp; dum&lt;-seq(0,1,by=(dum))*C<br />&nbsp;&nbsp;&nbsp; z1&lt;-c(-rev(dum[2:(nz1+1)]),dum)<br />&nbsp;&nbsp;&nbsp; Z=kronecker(diag(1,dim(X)[2]), t(z1))<br />&nbsp;&nbsp;&nbsp; ## inicial po.<br />&nbsp;&nbsp;&nbsp; p1o=rep(1,length(z1))<br />&nbsp;&nbsp;&nbsp; p1o=p1o/sum(p1o)<br />&nbsp;&nbsp;&nbsp; po&lt;-rep(p1o,dim(X)[2])</p>
<p>}</p>
<p>if (opt2==&#8221;prior&#8221;) {<br />&nbsp;&nbsp;&nbsp; z1=rep(0,M)<br />&nbsp;&nbsp;&nbsp; Z=kronecker(diag(1,dim(X)[2]), t(z1))<br />&nbsp;&nbsp;&nbsp; ## rownames<br />&nbsp;&nbsp;&nbsp; dum1&lt;-rep(&#8220;beta&#8221;,dim(X)[2])<br />&nbsp;&nbsp;&nbsp; dum2&lt;-seq(1:dim(X)[2])<br />&nbsp;&nbsp;&nbsp; dum1&lt;-paste(dum1,dum2,sep=&#8221;")<br />&nbsp;&nbsp;&nbsp; rownames(Z)&lt;-dum1<br />&nbsp;&nbsp;&nbsp; ## colnames<br />&nbsp;&nbsp;&nbsp; dum1&lt;-rep(&#8220;p&#8221;,M)<br />&nbsp;&nbsp;&nbsp; dum2&lt;-seq(1:M)<br />&nbsp;&nbsp;&nbsp; dum1&lt;-paste(dum1,dum2,sep=&#8221;")<br />&nbsp;&nbsp;&nbsp; dum2&lt;-rep(dum1,dim(X)[2])<br />&nbsp;&nbsp;&nbsp; colnames(Z)&lt;-dum2<br />&nbsp;&nbsp;&nbsp; ## <br />&nbsp;&nbsp;&nbsp; dum=0<br />&nbsp;&nbsp;&nbsp; for (i in 1:dim(X)[2]) {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dum1=1+dum<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Z[i,dum1:(dum1+(M-1))]&lt;-matprior[i,]<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dum=dum+(M)<br />&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; ## inicial po.<br />&nbsp;&nbsp;&nbsp; p1o=rep(0,length(z1))<br />&nbsp;&nbsp;&nbsp; po&lt;-rep(p1o,dim(X)[2])<br />&nbsp;&nbsp;&nbsp; dum=0<br />&nbsp;&nbsp;&nbsp; for (i in 1:dim(X)[2]) {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dum1=1+dum<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; po[dum1:(dum1+(M-1))]&lt;-matprior2[i,]<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dum=dum+M<br />&nbsp;&nbsp;&nbsp; }</p>
<p>}<br />&nbsp;&nbsp;&nbsp; <br />## Soportes para w, en este caso se va a utilizar la <br />## regla 3sigma. <br />## sigma es la desviaci?n est?ndar emp?rica de Y.<br />sigma=sd(Y)<br />## El soporte para J=5. <br />## El valor m?nimo debe ser -3*sigma<br />## El valor m?ximo debe ser 3*sigma<br />## Deben ser sim?tros alrrededor de cero.<br />## vt=(-3*sigma,-1.5*sigma,0,1.5*sigma,3*sigma)<br />v1&lt;-c(-3*sigma,-1.5*sigma,0,1.5*sigma,3*sigma)</p>
<p>V=kronecker(diag(1,length(Y)), t(v1))</p>
<p>## inicial po.<br />w1o=rep(1,length(v1))<br />w1o=w1o/sum(w1o)<br />wo&lt;-rep(w1o,length(Y))</p>
<p>## Valor inicial total.<br />pwo&lt;-c(po,wo)</p>
<p>## k <br />k=length(po)</p>
<p>## Equality restrictions.<br />## h[i]=0<br />heq&lt;-function(pw=pw,Z=Z,V=V,k=k,Y=Y,fun=fun,X=X) {<br />## pw vector de par?metros que concatena a p y w.<br />## Z, es la matriz de soportes de los Beta.<br />## V, es la matriz de soportes de los errores.<br />## k, longitud del par?metro p. <br />&nbsp; len=length(pw)<br />&nbsp;&nbsp;&nbsp; p=pw[1:k]<br />&nbsp;&nbsp;&nbsp; w=pw[(k+1):len]<br />&nbsp;&nbsp;&nbsp; ## tama?o de restricciones.<br />&nbsp;&nbsp;&nbsp; res.1&lt;-dim(V)[2]<br />&nbsp;&nbsp;&nbsp; ## restricciones de probabilidades.<br />&nbsp;&nbsp;&nbsp; res.2&lt;-2<br />&nbsp;&nbsp;&nbsp; res&lt;-res.1+res.2<br />&nbsp;&nbsp;&nbsp; h&lt;-rep(NA,res)<br />&nbsp;&nbsp;&nbsp; ## Restricciones lineales.<br />&nbsp;&nbsp;&nbsp; theta&lt;-(Z%*%p)<br />&nbsp;&nbsp;&nbsp; h[1:res.1]&lt;-fun(theta=theta,X=X)+(V%*%w)-Y<br />&nbsp;&nbsp;&nbsp; ## Restricciones de probabilidades.<br />&nbsp;&nbsp;&nbsp; h[(res.1+1)]&lt;-sum(p,na.rm=TRUE)-1<br />&nbsp;&nbsp;&nbsp; h[(res.1+2)]&lt;-sum(w,na.rm=TRUE)-1<br />&nbsp;&nbsp;&nbsp; h<br />}&nbsp;&nbsp;&nbsp; </p>
<p>## Inequality restrictions.<br />## h[i]&gt;0<br />hin&lt;-function(pw=pw,Z=Z,V=V,k=k,Y=Y,fun=fun,X=X) {<br />## pw vector de par?metros que concatena a p y w.<br />## Z, es la matriz de soportes de los Beta.<br />## V, es la matriz de soportes de los errores.<br />## k, longitud del par?metro p. <br />&nbsp;&nbsp;&nbsp; len=length(pw)<br />&nbsp;&nbsp;&nbsp; h&lt;-rep(NA,(2*len))<br />&nbsp;&nbsp;&nbsp; ## Restricciones lineales.<br />&nbsp;&nbsp;&nbsp; h[1:len]&lt;-pw<br />&nbsp;&nbsp;&nbsp; h[(len+1):(2*len)]&lt;- -pw+1<br />}&nbsp;&nbsp;&nbsp; </p>
<p>H.obj&lt;-function(pw=pw,Z=Z,V=V,k=k,Y=Y,fun=fun,X=X) {<br />## pw vector de par?metros que concatena a p y w.<br />## Z, es la matriz de soportes de los Beta.<br />## V, es la matriz de soportes de los errores.<br />## k, longitud del par?metro p. <br />&nbsp;&nbsp;&nbsp; len=length(pw)<br />&nbsp;&nbsp;&nbsp; p=pw[1:k]<br />&nbsp;&nbsp;&nbsp; w=pw[(k+1):len]<br />&nbsp;&nbsp;&nbsp; Objective=as.numeric(-(t(p)%*%log(p)))+as.numeric(-(t(w)%*%log(w)))<br />&nbsp;&nbsp;&nbsp; -Objective<br />}</p>
<p>## El default de auglag y optim es minimizar.<br />## gradiente, recibe R^n y bota R^n.<br />H.gr&lt;-function(pw=pw,Z=Z,V=V,k=k,Y=Y,fun=fun,X=X) {<br />## pw vector de par?metros que concatena a p y w.<br />## Z, es la matriz de soportes de los Beta.<br />## V, es la matriz de soportes de los errores.<br />## k, longitud del par?metro p. <br />&nbsp;&nbsp;&nbsp; len=length(pw)<br />&nbsp;&nbsp;&nbsp; p=pw[1:k]<br />&nbsp;&nbsp;&nbsp; w=pw[(k+1):len]<br />&nbsp;&nbsp;&nbsp; Grad.p&lt;-log(p)+1<br />&nbsp;&nbsp;&nbsp; Grad.w&lt;-log(w)+1<br />&nbsp;&nbsp;&nbsp; Grad&lt;-c(Grad.p,Grad.w)<br />&nbsp;&nbsp;&nbsp; Grad<br />}</p>
<p>## El default de auglag y optim es minimizar.</p>
<p>################################################################<br />## Optimizaci?n.<br />################################################################<br />if (opt1==&#8221;nongr&#8221;) {<br />&nbsp;&nbsp;&nbsp; results&lt;-constrOptim.nl(par=pwo, fn=H.obj, heq=heq,hin=hin,Z=Z,V=V,k=k,Y=Y,X=X,fun=fun)<br />}</p>
<p>if (opt1==&#8221;gr&#8221;) {<br />&nbsp;&nbsp;&nbsp; ## con gradiente<br />&nbsp;&nbsp;&nbsp; results&lt;-constrOptim.nl(par=pwo, fn=H.obj, gr=H.gr, heq=heq,hin=hin,Z=Z,V=V,k=k,Y=Y,X=X,fun=fun)<br />}<br />##beta<br />## recobro los p del par=pw y multiplico por el soporte Z, para obtener el vector de coeficientes.<br />beta&lt;-Z%*%results$par[1:k]</p>
<p>e&lt;-V%*%results$par[(k+1):length(results$par)]</p>
<p>&nbsp;&nbsp;&nbsp; list(beta=beta,e=e)<br />}</p>
<p>#gme.nl=cmpfun(gme.nl)<br />##<br />##<br />## Generalized Maximum Entropy no lineal v2 especializado para stock de capital.</p>
<p>gme.nl2=function(Y=Y,X=X,M=M,C=C,fun=fun,opt1=opt1,opt2=opt2,matprior=matprior,matprior2=matprior2) {<br />## inicializacion de I y L<br />&nbsp; I=X[,"I"]<br />&nbsp; L=X[,"L"]<br />&nbsp; <br />## Support Matrix of dim k x M.</p>
<p>if (opt2==&#8221;noprior&#8221;) {&nbsp; <br />&nbsp; z1=rep(0,M)<br />&nbsp;&nbsp;&nbsp; nz1=floor(length(z1)/2)<br />&nbsp;&nbsp;&nbsp; dum&lt;-1/nz1<br />&nbsp;&nbsp;&nbsp; dum&lt;-seq(0,1,by=(dum))*C<br />&nbsp;&nbsp;&nbsp; z1&lt;-c(-rev(dum[2:(nz1+1)]),dum)<br />&nbsp;&nbsp;&nbsp; Z=kronecker(diag(1,dim(X)[2]), t(z1))<br />&nbsp;&nbsp;&nbsp; ## inicial po.<br />&nbsp;&nbsp;&nbsp; p1o=rep(1,length(z1))<br />&nbsp;&nbsp;&nbsp; p1o=p1o/sum(p1o)<br />&nbsp;&nbsp;&nbsp; po&lt;-rep(p1o,dim(X)[2])</p>
<p>}</p>
<p>if (opt2==&#8221;prior&#8221;) {<br />&nbsp;&nbsp;&nbsp; z1=rep(0,M)<br />&nbsp;&nbsp;&nbsp; Z=kronecker(diag(1,dim(X)[2]), t(z1))<br />&nbsp;&nbsp;&nbsp; ## rownames<br />&nbsp;&nbsp;&nbsp; dum1&lt;-rep(&#8220;beta&#8221;,dim(X)[2])<br />&nbsp;&nbsp;&nbsp; dum2&lt;-seq(1:dim(X)[2])<br />&nbsp;&nbsp;&nbsp; dum1&lt;-paste(dum1,dum2,sep=&#8221;")<br />&nbsp;&nbsp;&nbsp; rownames(Z)&lt;-dum1<br />&nbsp;&nbsp;&nbsp; ## colnames<br />&nbsp;&nbsp;&nbsp; dum1&lt;-rep(&#8220;p&#8221;,M)<br />&nbsp;&nbsp;&nbsp; dum2&lt;-seq(1:M)<br />&nbsp;&nbsp;&nbsp; dum1&lt;-paste(dum1,dum2,sep=&#8221;")<br />&nbsp;&nbsp;&nbsp; dum2&lt;-rep(dum1,dim(X)[2])<br />&nbsp;&nbsp;&nbsp; colnames(Z)&lt;-dum2<br />&nbsp;&nbsp;&nbsp; ## <br />&nbsp;&nbsp;&nbsp; dum=0<br />&nbsp;&nbsp;&nbsp; for (i in 1:dim(X)[2]) {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dum1=1+dum<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Z[i,dum1:(dum1+(M-1))]&lt;-matprior[i,]<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dum=dum+(M)<br />&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; ## inicial po.<br />&nbsp;&nbsp;&nbsp; p1o=rep(0,length(z1))<br />&nbsp;&nbsp;&nbsp; po&lt;-rep(p1o,dim(X)[2])<br />&nbsp;&nbsp;&nbsp; dum=0<br />&nbsp;&nbsp;&nbsp; for (i in 1:dim(X)[2]) {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dum1=1+dum<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; po[dum1:(dum1+(M-1))]&lt;-matprior2[i,]<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dum=dum+M<br />&nbsp;&nbsp;&nbsp; }</p>
<p>}</p>
<p>&nbsp; <br />## Matriz para obtener restricciones de probabilidades<br />z.id=rep(1,M)<br />Idp=kronecker(diag(1,dim(X)[2]), t(z.id))<br />&nbsp;&nbsp;&nbsp; <br />## Soportes para w, en este caso se va a utilizar la <br />## regla 3sigma. <br />## sigma es la desviaci?n est?ndar emp?rica de Y.<br />sigma=sd(Y)<br />## El soporte para J=5. <br />## El valor m?nimo debe ser -3*sigma<br />## El valor m?ximo debe ser 3*sigma<br />## Deben ser sim?tros alrrededor de cero.<br />## vt=(-3*sigma,-1.5*sigma,0,1.5*sigma,3*sigma)<br />v1&lt;-c(-3*sigma,-1.5*sigma,0,1.5*sigma,3*sigma)</p>
<p>V=kronecker(diag(1,length(Y)), t(v1))</p>
<p>&nbsp; <br />## Matriz para obtener restricciones de probabilidades<br />v1.id=rep(1,length(v1))<br />Idw=kronecker(diag(1,length(Y)), t(v1.id))<br />&nbsp; <br />## inicial po.<br />w1o=rep(1,length(v1))<br />w1o=w1o/sum(w1o)<br />wo&lt;-rep(w1o,length(Y))</p>
<p>## Valor inicial total.<br />pwo&lt;-c(po,wo)</p>
<p>## k <br />k=length(po)</p>
<p>## Equality restrictions.<br />## h[i]=0<br />heq&lt;-function(pw=pw,Z=Z,V=V,k=k,Y=Y,fun=fun,X=X) {<br />## pw vector de par?metros que concatena a p y w.<br />## Z, es la matriz de soportes de los Beta.<br />## V, es la matriz de soportes de los errores.<br />## k, longitud del par?metro p. <br />&nbsp; len=length(pw)<br />&nbsp;&nbsp;&nbsp; p=pw[1:k]<br />&nbsp;&nbsp;&nbsp; w=pw[(k+1):len]<br />&nbsp;&nbsp;&nbsp; ## tama?o de restricciones.<br />&nbsp;&nbsp;&nbsp; res.1&lt;-length(Y)<br />&nbsp;&nbsp;&nbsp; ## restricciones de probabilidades.<br />&nbsp;&nbsp;&nbsp; res.2&lt;-dim(Z)[1]<br />&nbsp;&nbsp;&nbsp; res&lt;-res.1+res.2+res.1<br />&nbsp;&nbsp;&nbsp; h&lt;-rep(NA,res)<br />&nbsp;&nbsp;&nbsp; ## Restricciones lineales.<br />&nbsp;&nbsp;&nbsp; theta&lt;-(Z%*%p)<br />&nbsp;&nbsp;&nbsp; h[1:res.1]&lt;-fun(theta=theta,X=X)+(V%*%w)-Y<br />&nbsp;&nbsp;&nbsp; ## Restricciones de probabilidades.<br />&nbsp;&nbsp;&nbsp; h[(res.1+1):(res.1+(res.2))]&lt;-as.numeric(Idp%*%p-rep(1,times=(res.2/2)))<br />&nbsp;&nbsp;&nbsp; h[(res.1+(res.2)+1):length(h)]&lt;-as.numeric(Idw%*%w-rep(1,times=res.1))<br />&nbsp;&nbsp;&nbsp; h<br />}&nbsp;&nbsp;&nbsp; </p>
<p>## Inequality restrictions.<br />## h[i]&gt;0<br />hin&lt;-function(pw=pw,Z=Z,V=V,k=k,Y=Y,fun=fun,X=X) {<br />## pw vector de par?metros que concatena a p y w.<br />## Z, es la matriz de soportes de los Beta.<br />## V, es la matriz de soportes de los errores.<br />## k, longitud del par?metro p. <br />&nbsp;&nbsp;&nbsp; len=length(pw)<br />&nbsp;&nbsp;&nbsp; h&lt;-rep(NA,(2*len))<br />&nbsp;&nbsp;&nbsp; ## Restricciones lineales.<br />&nbsp;&nbsp;&nbsp; h[1:len]&lt;-pw<br />&nbsp;&nbsp;&nbsp; h[(len+1):(2*len)]&lt;- -pw+1<br />}&nbsp;&nbsp;&nbsp; </p>
<p>H.obj&lt;-function(pw=pw,Z=Z,V=V,k=k,Y=Y,fun=fun,X=X) {<br />## pw vector de par?metros que concatena a p y w.<br />## Z, es la matriz de soportes de los Beta.<br />## V, es la matriz de soportes de los errores.<br />## k, longitud del par?metro p. <br />&nbsp;&nbsp;&nbsp; len=length(pw)<br />&nbsp;&nbsp;&nbsp; p=pw[1:k]<br />&nbsp;&nbsp;&nbsp; w=pw[(k+1):len]<br />&nbsp;&nbsp;&nbsp; Objective=as.numeric(-(t(p)%*%log(p)))+as.numeric(-(t(w)%*%log(w)))<br />&nbsp;&nbsp;&nbsp; -Objective<br />}</p>
<p>## El default de auglag y optim es minimizar.<br />## gradiente, recibe R^n y bota R^n.<br />H.gr&lt;-function(pw=pw,Z=Z,V=V,k=k,Y=Y,fun=fun,X=X) {<br />## pw vector de par?metros que concatena a p y w.<br />## Z, es la matriz de soportes de los Beta.<br />## V, es la matriz de soportes de los errores.<br />## k, longitud del par?metro p. <br />&nbsp;&nbsp;&nbsp; len=length(pw)<br />&nbsp;&nbsp;&nbsp; p=pw[1:k]<br />&nbsp;&nbsp;&nbsp; w=pw[(k+1):len]<br />&nbsp;&nbsp;&nbsp; Grad.p&lt;-log(p)+1<br />&nbsp;&nbsp;&nbsp; Grad.w&lt;-log(w)+1<br />&nbsp;&nbsp;&nbsp; Grad&lt;-c(Grad.p,Grad.w)<br />&nbsp;&nbsp;&nbsp; Grad<br />}</p>
<p>## El default de auglag y optim es minimizar.</p>
<p>################################################################<br />## Optimizaci?n.<br />################################################################<br />if (opt1==&#8221;nongr&#8221;) {<br />&nbsp;&nbsp;&nbsp; results&lt;-constrOptim.nl(par=pwo, fn=H.obj, heq=heq,hin=hin,Z=Z,V=V,k=k,Y=Y,X=X,fun=fun)<br />}</p>
<p>if (opt1==&#8221;gr&#8221;) {<br />&nbsp;&nbsp;&nbsp; ## con gradiente<br />&nbsp;&nbsp;&nbsp; results&lt;-constrOptim.nl(par=pwo, fn=H.obj, gr=H.gr, heq=heq,hin=hin,Z=Z,V=V,k=k,Y=Y,X=X,fun=fun)<br />}<br />##beta<br />## recobro los p del par=pw y multiplico por el soporte Z, para obtener el vector de coeficientes.<br />beta&lt;-Z%*%results$par[1:k]</p>
<p>e&lt;-V%*%results$par[(k+1):length(results$par)]</p>
<p>Hn=-results$value/log(length(results$par))</p>
<p>&nbsp; list(beta=beta,e=e,Hn=Hn)<br />}</p>
<p>#gme.nl=cmpfun(gme.nl)</p>
<p>gme.v2=function(Y=Y,X=X,M=M,C=C,opt1=opt1,opt2=opt2,matprior=matprior,matprior2=matprior2) {<br />&nbsp; ## Support Matrix of dim k x M.</p>
<p>if (opt2==&#8221;noprior&#8221;) {&nbsp; <br />&nbsp;&nbsp;&nbsp; z1=rep(0,M)<br />&nbsp;&nbsp;&nbsp; nz1=floor(length(z1)/2)<br />&nbsp;&nbsp;&nbsp; dum&lt;-1/nz1<br />&nbsp;&nbsp;&nbsp; dum&lt;-seq(0,1,by=(dum))*C<br />&nbsp;&nbsp;&nbsp; z1&lt;-c(-rev(dum[2:(nz1+1)]),dum)<br />&nbsp;&nbsp;&nbsp; Z=kronecker(diag(1,dim(X)[2]), t(z1))<br />&nbsp;&nbsp;&nbsp; ## inicial po.<br />&nbsp;&nbsp;&nbsp; p1o=rep(1,length(z1))<br />&nbsp;&nbsp;&nbsp; p1o=p1o/sum(p1o)<br />&nbsp;&nbsp;&nbsp; po&lt;-rep(p1o,dim(X)[2])</p>
<p>}</p>
<p>if (opt2==&#8221;prior&#8221;) {<br />&nbsp;&nbsp;&nbsp; z1=rep(0,M)<br />&nbsp;&nbsp;&nbsp; Z=kronecker(diag(1,dim(X)[2]), t(z1))<br />&nbsp;&nbsp;&nbsp; ## rownames<br />&nbsp;&nbsp;&nbsp; dum1&lt;-rep(&#8220;beta&#8221;,dim(X)[2])<br />&nbsp;&nbsp;&nbsp; dum2&lt;-seq(1:dim(X)[2])<br />&nbsp;&nbsp;&nbsp; dum1&lt;-paste(dum1,dum2,sep=&#8221;")<br />&nbsp;&nbsp;&nbsp; rownames(Z)&lt;-dum1<br />&nbsp;&nbsp;&nbsp; ## colnames<br />&nbsp;&nbsp;&nbsp; dum1&lt;-rep(&#8220;p&#8221;,M)<br />&nbsp;&nbsp;&nbsp; dum2&lt;-seq(1:M)<br />&nbsp;&nbsp;&nbsp; dum1&lt;-paste(dum1,dum2,sep=&#8221;")<br />&nbsp;&nbsp;&nbsp; dum2&lt;-rep(dum1,dim(X)[2])<br />&nbsp;&nbsp;&nbsp; colnames(Z)&lt;-dum2<br />&nbsp;&nbsp;&nbsp; ## <br />&nbsp;&nbsp;&nbsp; dum=0<br />&nbsp;&nbsp;&nbsp; for (i in 1:dim(X)[2]) {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dum1=1+dum<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Z[i,dum1:(dum1+(M-1))]&lt;-matprior[i,]<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dum=dum+(M)<br />&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; ## inicial po.<br />&nbsp;&nbsp;&nbsp; p1o=rep(0,length(z1))<br />&nbsp;&nbsp;&nbsp; po&lt;-rep(p1o,dim(X)[2])<br />&nbsp;&nbsp;&nbsp; dum=0<br />&nbsp;&nbsp;&nbsp; for (i in 1:dim(X)[2]) {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dum1=1+dum<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; po[dum1:(dum1+(M-1))]&lt;-matprior2[i,]<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dum=dum+M<br />&nbsp;&nbsp;&nbsp; }</p>
<p>}<br />&nbsp;&nbsp;&nbsp; <br />## Soportes para w, en este caso se va a utilizar la <br />## regla 3sigma. <br />## sigma es la desviaci?n est?ndar emp?rica de Y.<br />sigma=sd(Y)<br />## El soporte para J=5. <br />## El valor m?nimo debe ser -3*sigma<br />## El valor m?ximo debe ser 3*sigma<br />## Deben ser sim?tros alrrededor de cero.<br />## vt=(-3*sigma,-1.5*sigma,0,1.5*sigma,3*sigma)<br />v1&lt;-c(-3*sigma,-1.5*sigma,0,1.5*sigma,3*sigma)</p>
<p>V=kronecker(diag(1,length(Y)), t(v1))</p>
<p>## inicial po.<br />w1o=rep(1,length(v1))<br />w1o=w1o/sum(w1o)<br />wo&lt;-rep(w1o,length(Y))</p>
<p>## Valor inicial total.<br />pwo&lt;-c(po,wo)</p>
<p>## k <br />k=length(po)<br />&nbsp; <br />&nbsp; ##&nbsp; matrices de restricciones<br />&nbsp; ## Matriz para obtener restricciones de probabilidades<br />&nbsp; v1.id=rep(1,length(v1))<br />&nbsp; Idw=kronecker(diag(1,length(Y)), t(v1.id))<br />&nbsp; ## Matriz para obtener restricciones de probabilidades<br />&nbsp; z.id=rep(1,M)<br />&nbsp; Idp=kronecker(diag(1,dim(X)[2]), t(z.id))</p>
<p>## Equality restrictions.<br />## h[i]=0<br />heq&lt;-function(pw=pw,Z=Z,V=V,k=k,Y=Y,X=X) {<br />## pw vector de par?metros que concatena a p y w.<br />## Z, es la matriz de soportes de los Beta.<br />## V, es la matriz de soportes de los errores.<br />## k, longitud del par?metro p. <br />&nbsp;&nbsp;&nbsp; len=length(pw)<br />&nbsp;&nbsp;&nbsp; p=pw[1:k]<br />&nbsp;&nbsp;&nbsp; w=pw[(k+1):len]<br />&nbsp;&nbsp;&nbsp; ## tama?o de restricciones.<br />&nbsp;&nbsp;&nbsp; res.1&lt;-length(Y)<br />&nbsp;&nbsp;&nbsp; ## restricciones de probabilidades.<br />&nbsp;&nbsp;&nbsp; res.2&lt;-dim(Z)[1]<br />&nbsp;&nbsp;&nbsp; res&lt;-res.1+res.2+res.1<br />&nbsp;&nbsp;&nbsp; h&lt;-rep(NA,res)<br />&nbsp;&nbsp;&nbsp; ## Restricciones lineales.<br />&nbsp;&nbsp;&nbsp; h[1:res.1]&lt;-X%*%(Z%*%p)+(V%*%w)-Y<br />&nbsp;&nbsp;&nbsp; ## Restricciones de probabilidades.<br />&nbsp;&nbsp;&nbsp; ## Restricciones de probabilidades.<br />&nbsp; h[(res.1+1):(res.1+(res.2))]&lt;-as.numeric(Idp%*%p-rep(1,times=(res.2/2)))<br />&nbsp;&nbsp;&nbsp; h[(res.1+(res.2)+1):length(h)]&lt;-as.numeric(Idw%*%w-rep(1,times=res.1))<br />&nbsp;&nbsp;&nbsp; h<br />}&nbsp;&nbsp;&nbsp; </p>
<p>## Inequality restrictions.<br />## h[i]&gt;0<br />hin&lt;-function(pw=pw,Z=Z,V=V,k=k,Y=Y,X=X) {<br />## pw vector de par?metros que concatena a p y w.<br />## Z, es la matriz de soportes de los Beta.<br />## V, es la matriz de soportes de los errores.<br />## k, longitud del par?metro p. <br />&nbsp;&nbsp;&nbsp; len=length(pw)<br />&nbsp;&nbsp;&nbsp; h&lt;-rep(NA,(2*len))<br />&nbsp;&nbsp;&nbsp; ## Restricciones lineales.<br />&nbsp;&nbsp;&nbsp; h[1:len]&lt;-pw<br />&nbsp;&nbsp;&nbsp; h[(len+1):(2*len)]&lt;- -pw+1<br />}&nbsp;&nbsp;&nbsp; </p>
<p>H.obj&lt;-function(pw=pw,Z=Z,V=V,k=k,Y=Y,X=X) {<br />## pw vector de par?metros que concatena a p y w.<br />## Z, es la matriz de soportes de los Beta.<br />## V, es la matriz de soportes de los errores.<br />## k, longitud del par?metro p. <br />&nbsp;&nbsp;&nbsp; len=length(pw)<br />&nbsp;&nbsp;&nbsp; p=pw[1:k]<br />&nbsp;&nbsp;&nbsp; w=pw[(k+1):len]<br />&nbsp;&nbsp;&nbsp; Objective=as.numeric(-(t(p)%*%log(p)))+as.numeric(-(t(w)%*%log(w)))<br />&nbsp;&nbsp;&nbsp; -Objective<br />}</p>
<p>## gradiente, recibe R^n y bota R^n.<br />H.gr&lt;-function(pw=pw,Z=Z,V=V,k=k,Y=Y,X=X) {<br />## pw vector de par?metros que concatena a p y w.<br />## Z, es la matriz de soportes de los Beta.<br />## V, es la matriz de soportes de los errores.<br />## k, longitud del par?metro p. <br />&nbsp;&nbsp;&nbsp; len=length(pw)<br />&nbsp;&nbsp;&nbsp; p=pw[1:k]<br />&nbsp;&nbsp;&nbsp; w=pw[(k+1):len]<br />&nbsp;&nbsp;&nbsp; Grad.p&lt;-log(p)+1<br />&nbsp;&nbsp;&nbsp; Grad.w&lt;-log(w)+1<br />&nbsp;&nbsp;&nbsp; Grad&lt;-c(Grad.p,Grad.w)<br />&nbsp;&nbsp;&nbsp; Grad<br />}<br />## El default de auglag y optim es minimizar.</p>
<p>################################################################<br />## Optimizaci?n.<br />################################################################<br />if (opt1==&#8221;nongr&#8221;) {<br />&nbsp;&nbsp;&nbsp; results&lt;-constrOptim.nl(par=pwo, fn=H.obj, heq=heq,hin=hin,Z=Z,V=V,k=k,Y=Y,X=X)<br />}</p>
<p>if (opt1==&#8221;gr&#8221;) {<br />&nbsp;&nbsp;&nbsp; ## con gradiente<br />&nbsp;&nbsp;&nbsp; results&lt;-constrOptim.nl(par=pwo, fn=H.obj, gr=H.gr, heq=heq,hin=hin,Z=Z,V=V,k=k,Y=Y,X=X)<br />}<br />##beta<br />## recobro los p del par=pw y multiplico por el soporte Z, para obtener el vector de coeficientes.<br />beta&lt;-Z%*%results$par[1:k]</p>
<p>e&lt;-V%*%results$par[(k+1):length(results$par)]</p>
<p>Hn&lt;&#8211;results$value</p>
<p>&nbsp;&nbsp;&nbsp; list(beta=beta,e=e,Hn=Hn)<br />}</p>
<p>#&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br /># entropia normalizada, recordad que la distribucion uniforme maximiza la entropia 1/n, entonces <br /># se normaliza la entropia dividiendo para ln n . Hn = H/ln n <br /># -results$fn/log(dim(X)[1])<br />## Generalized Maximum Entropy no lineal v2 especializado para stock de capital.</p>
<p>gme.nl3=function(Y=Y,X=X,M=M,C=C,fun=fun,opt1=opt1,opt2=opt2,matprior=matprior,matprior2=matprior2,fix.t=fix.t) {<br />## inicializacion de I y L<br />&nbsp; I=X[,"I"]<br />&nbsp; L=X[,"L"]<br />&nbsp; <br />## Support Matrix of dim k x M.</p>
<p>if (opt2==&#8221;noprior&#8221;) {&nbsp; <br />&nbsp; z1=rep(0,M)<br />&nbsp; nz1=floor(length(z1)/2)<br />&nbsp;&nbsp;&nbsp; dum&lt;-1/nz1<br />&nbsp;&nbsp;&nbsp; dum&lt;-seq(0,1,by=(dum))*C<br />&nbsp;&nbsp;&nbsp; z1&lt;-c(-rev(dum[2:(nz1+1)]),dum)<br />&nbsp;&nbsp;&nbsp; Z=kronecker(diag(1,dim(X)[2]), t(z1))<br />&nbsp;&nbsp;&nbsp; ## inicial po.<br />&nbsp;&nbsp;&nbsp; p1o=rep(1,length(z1))<br />&nbsp;&nbsp;&nbsp; p1o=p1o/sum(p1o)<br />&nbsp;&nbsp;&nbsp; po&lt;-rep(p1o,dim(X)[2])</p>
<p>}</p>
<p>if (opt2==&#8221;prior&#8221;) {<br />&nbsp;&nbsp;&nbsp; z1=rep(0,M)<br />&nbsp;&nbsp;&nbsp; Z=kronecker(diag(1,dim(X)[2]), t(z1))<br />&nbsp;&nbsp;&nbsp; ## rownames<br />&nbsp;&nbsp;&nbsp; dum1&lt;-rep(&#8220;beta&#8221;,dim(X)[2])<br />&nbsp;&nbsp;&nbsp; dum2&lt;-seq(1:dim(X)[2])<br />&nbsp;&nbsp;&nbsp; dum1&lt;-paste(dum1,dum2,sep=&#8221;")<br />&nbsp;&nbsp;&nbsp; rownames(Z)&lt;-dum1<br />&nbsp;&nbsp;&nbsp; ## colnames<br />&nbsp;&nbsp;&nbsp; dum1&lt;-rep(&#8220;p&#8221;,M)<br />&nbsp;&nbsp;&nbsp; dum2&lt;-seq(1:M)<br />&nbsp;&nbsp;&nbsp; dum1&lt;-paste(dum1,dum2,sep=&#8221;")<br />&nbsp;&nbsp;&nbsp; dum2&lt;-rep(dum1,dim(X)[2])<br />&nbsp;&nbsp;&nbsp; colnames(Z)&lt;-dum2<br />&nbsp;&nbsp;&nbsp; ## <br />&nbsp;&nbsp;&nbsp; dum=0<br />&nbsp;&nbsp;&nbsp; for (i in 1:dim(X)[2]) {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dum1=1+dum<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Z[i,dum1:(dum1+(M-1))]&lt;-matprior[i,]<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dum=dum+(M)<br />&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; ## inicial po.<br />&nbsp;&nbsp;&nbsp; p1o=rep(0,length(z1))<br />&nbsp;&nbsp;&nbsp; po&lt;-rep(p1o,dim(X)[2])<br />&nbsp;&nbsp;&nbsp; dum=0<br />&nbsp;&nbsp;&nbsp; for (i in 1:dim(X)[2]) {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dum1=1+dum<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; po[dum1:(dum1+(M-1))]&lt;-matprior2[i,]<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dum=dum+M<br />&nbsp;&nbsp;&nbsp; }</p>
<p>}</p>
<p>&nbsp; <br />## Matriz para obtener restricciones de probabilidades<br />z.id=rep(1,M)<br />Idp=kronecker(diag(1,dim(X)[2]), t(z.id))<br />&nbsp;&nbsp;&nbsp; <br />## Soportes para w, en este caso se va a utilizar la <br />## regla 3sigma. <br />## sigma es la desviaci?n est?ndar emp?rica de Y.<br />sigma=sd(Y)<br />## El soporte para J=5. <br />## El valor m?nimo debe ser -3*sigma<br />## El valor m?ximo debe ser 3*sigma<br />## Deben ser sim?tros alrrededor de cero.<br />## vt=(-3*sigma,-1.5*sigma,0,1.5*sigma,3*sigma)<br />v1&lt;-c(-3*sigma,-1.5*sigma,0,1.5*sigma,3*sigma)</p>
<p>V=kronecker(diag(1,length(Y)), t(v1))</p>
<p>&nbsp; <br />## Matriz para obtener restricciones de probabilidades<br />v1.id=rep(1,length(v1))<br />Idw=kronecker(diag(1,length(Y)), t(v1.id))<br />&nbsp; <br />## inicial po.<br />w1o=rep(1,length(v1))<br />w1o=w1o/sum(w1o)<br />wo&lt;-rep(w1o,length(Y))</p>
<p>## Valor inicial total.<br />pwo&lt;-c(po,wo)</p>
<p>## k <br />k=length(po)</p>
<p>## Equality restrictions.<br />## h[i]=0<br />heq&lt;-function(pw=pw,Z=Z,V=V,k=k,Y=Y,fun=fun,X=X) {<br />## pw vector de par?metros que concatena a p y w.<br />## Z, es la matriz de soportes de los Beta.<br />## V, es la matriz de soportes de los errores.<br />## k, longitud del par?metro p. <br />&nbsp; len=length(pw)<br />&nbsp;&nbsp;&nbsp; p=pw[1:k]<br />&nbsp;&nbsp;&nbsp; w=pw[(k+1):len]<br />&nbsp;&nbsp;&nbsp; ## tama?o de restricciones.<br />&nbsp;&nbsp;&nbsp; res.1&lt;-length(Y)<br />&nbsp;&nbsp;&nbsp; ## restricciones de probabilidades.<br />&nbsp;&nbsp;&nbsp; res.2&lt;-dim(Z)[1]<br />&nbsp;&nbsp;&nbsp; res&lt;-res.1+res.2+res.1<br />&nbsp;&nbsp;&nbsp; h&lt;-rep(NA,res)<br />&nbsp;&nbsp;&nbsp; ## Restricciones lineales.<br />&nbsp;&nbsp;&nbsp; theta&lt;-(Z%*%p)<br />&nbsp;&nbsp;&nbsp; h[1:res.1]&lt;-fun(theta=theta,X=X)+(V%*%w)-Y<br />&nbsp;&nbsp;&nbsp; ## Restricciones de probabilidades.<br />&nbsp;&nbsp;&nbsp; h[(res.1+1):(res.1+(res.2))]&lt;-as.numeric(Idp%*%p-rep(1,times=(res.2/2)))<br />&nbsp;&nbsp;&nbsp; h[(res.1+(res.2)+1):length(h)]&lt;-as.numeric(Idw%*%w-rep(1,times=res.1))<br />&nbsp;&nbsp;&nbsp; <br />&nbsp; # fix.t<br />&nbsp; hf&lt;-as.numeric(V%*%w)[fix.t]<br />&nbsp; h&lt;-c(h,hf)<br />&nbsp; <br />&nbsp; h<br />}&nbsp;&nbsp;&nbsp; </p>
<p>## Inequality restrictions.<br />## h[i]&gt;0<br />hin&lt;-function(pw=pw,Z=Z,V=V,k=k,Y=Y,fun=fun,X=X) {<br />## pw vector de par?metros que concatena a p y w.<br />## Z, es la matriz de soportes de los Beta.<br />## V, es la matriz de soportes de los errores.<br />## k, longitud del par?metro p. <br />&nbsp;&nbsp;&nbsp; len=length(pw)<br />&nbsp;&nbsp;&nbsp; h&lt;-rep(NA,(2*len))<br />&nbsp;&nbsp;&nbsp; ## Restricciones lineales.<br />&nbsp;&nbsp;&nbsp; h[1:len]&lt;-pw<br />&nbsp;&nbsp;&nbsp; h[(len+1):(2*len)]&lt;- -pw+1<br />}&nbsp;&nbsp;&nbsp; </p>
<p>H.obj&lt;-function(pw=pw,Z=Z,V=V,k=k,Y=Y,fun=fun,X=X) {<br />## pw vector de par?metros que concatena a p y w.<br />## Z, es la matriz de soportes de los Beta.<br />## V, es la matriz de soportes de los errores.<br />## k, longitud del par?metro p. <br />&nbsp;&nbsp;&nbsp; len=length(pw)<br />&nbsp;&nbsp;&nbsp; p=pw[1:k]<br />&nbsp;&nbsp;&nbsp; w=pw[(k+1):len]<br />&nbsp;&nbsp;&nbsp; Objective=as.numeric(-(t(p)%*%log(p)))+as.numeric(-(t(w)%*%log(w)))<br />&nbsp;&nbsp;&nbsp; -Objective<br />}</p>
<p>## El default de auglag y optim es minimizar.<br />## gradiente, recibe R^n y bota R^n.<br />H.gr&lt;-function(pw=pw,Z=Z,V=V,k=k,Y=Y,fun=fun,X=X) {<br />## pw vector de par?metros que concatena a p y w.<br />## Z, es la matriz de soportes de los Beta.<br />## V, es la matriz de soportes de los errores.<br />## k, longitud del par?metro p. <br />&nbsp;&nbsp;&nbsp; len=length(pw)<br />&nbsp;&nbsp;&nbsp; p=pw[1:k]<br />&nbsp;&nbsp;&nbsp; w=pw[(k+1):len]<br />&nbsp;&nbsp;&nbsp; Grad.p&lt;-log(p)+1<br />&nbsp;&nbsp;&nbsp; Grad.w&lt;-log(w)+1<br />&nbsp;&nbsp;&nbsp; Grad&lt;-c(Grad.p,Grad.w)<br />&nbsp;&nbsp;&nbsp; Grad<br />}</p>
<p>## El default de auglag y optim es minimizar.</p>
<p>################################################################<br />## Optimizaci?n.<br />################################################################<br />if (opt1==&#8221;nongr&#8221;) {<br />&nbsp;&nbsp;&nbsp; results&lt;-constrOptim.nl(par=pwo, fn=H.obj, heq=heq,hin=hin,Z=Z,V=V,k=k,Y=Y,X=X,fun=fun)<br />}</p>
<p>if (opt1==&#8221;gr&#8221;) {<br />&nbsp;&nbsp;&nbsp; ## con gradiente<br />&nbsp;&nbsp;&nbsp; results&lt;-constrOptim.nl(par=pwo, fn=H.obj, gr=H.gr, heq=heq,hin=hin,Z=Z,V=V,k=k,Y=Y,X=X,fun=fun)<br />}<br />##beta<br />## recobro los p del par=pw y multiplico por el soporte Z, para obtener el vector de coeficientes.<br />beta&lt;-Z%*%results$par[1:k]</p>
<p>e&lt;-V%*%results$par[(k+1):length(results$par)]</p>
<p>Hn=-results$value/log(length(results$par))</p>
<p>&nbsp; list(beta=beta,e=e,Hn=Hn)<br />}</p>
<p></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vhaguiar.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vhaguiar.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vhaguiar.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vhaguiar.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vhaguiar.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vhaguiar.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vhaguiar.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vhaguiar.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vhaguiar.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vhaguiar.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vhaguiar.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vhaguiar.wordpress.com/135/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vhaguiar.wordpress.com/135/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vhaguiar.wordpress.com/135/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vhaguiar.wordpress.com&amp;blog=7156458&amp;post=135&amp;subd=vhaguiar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vhaguiar.wordpress.com/2011/12/29/r-maximum-entropy-regression-linear-and-non-linear-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7b3dce594765a38d0aad4c98c9f342a1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vhaguiar</media:title>
		</media:content>
	</item>
		<item>
		<title>Ubuntu: corrupted Synaptic.</title>
		<link>http://vhaguiar.wordpress.com/2011/08/12/ubuntu-corrupted-synaptic/</link>
		<comments>http://vhaguiar.wordpress.com/2011/08/12/ubuntu-corrupted-synaptic/#comments</comments>
		<pubDate>Fri, 12 Aug 2011 10:26:44 +0000</pubDate>
		<dc:creator>vhaguiar</dc:creator>
				<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://vhaguiar.wordpress.com/2011/08/12/ubuntu-corrupted-synaptic/</guid>
		<description><![CDATA[If your Synaptic is corrupted you can follow the instructions in this forum<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vhaguiar.wordpress.com&amp;blog=7156458&amp;post=130&amp;subd=vhaguiar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If your Synaptic is corrupted you can follow the instructions in this <a href="http://ubuntuforums.org/archive/index.php/t-160331.html">forum</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vhaguiar.wordpress.com/130/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vhaguiar.wordpress.com/130/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vhaguiar.wordpress.com/130/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vhaguiar.wordpress.com/130/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vhaguiar.wordpress.com/130/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vhaguiar.wordpress.com/130/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vhaguiar.wordpress.com/130/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vhaguiar.wordpress.com/130/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vhaguiar.wordpress.com/130/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vhaguiar.wordpress.com/130/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vhaguiar.wordpress.com/130/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vhaguiar.wordpress.com/130/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vhaguiar.wordpress.com/130/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vhaguiar.wordpress.com/130/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vhaguiar.wordpress.com&amp;blog=7156458&amp;post=130&amp;subd=vhaguiar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vhaguiar.wordpress.com/2011/08/12/ubuntu-corrupted-synaptic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7b3dce594765a38d0aad4c98c9f342a1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vhaguiar</media:title>
		</media:content>
	</item>
		<item>
		<title>Stata: Install Stata 10 MP dynamically linked (gui) in Ubuntu Natty</title>
		<link>http://vhaguiar.wordpress.com/2011/07/31/stata-install-stata-10-mp-dynamically-linked-gui-in-ubuntu-natty/</link>
		<comments>http://vhaguiar.wordpress.com/2011/07/31/stata-install-stata-10-mp-dynamically-linked-gui-in-ubuntu-natty/#comments</comments>
		<pubDate>Sun, 31 Jul 2011 22:35:55 +0000</pubDate>
		<dc:creator>vhaguiar</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[Stata]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://vhaguiar.wordpress.com/2011/07/31/stata-install-stata-10-mp-dynamically-linked-gui-in-ubuntu-natty/</guid>
		<description><![CDATA[This post draws heavily from andrewdyck blog . You need unix installation files for Stata 11 of course, then open a terminal and: sudo mkdir /usr/local/stata10cd /usr/local/stata10sh /media/cdrom/installDont forget to give permissions to read and write and create and delete files to this folder. sudo chmod -R 755 /usr/local/stata10Alternatively, use gksudo nautilus and create the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vhaguiar.wordpress.com&amp;blog=7156458&amp;post=126&amp;subd=vhaguiar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This post draws heavily from <a href="http://www.andrewdyck.com/installing-stata-on-ubuntu-or-linux-mint/">andrewdyck blog</a> . <br />You need unix installation files for Stata 11 of course, then open a terminal and: <br />sudo mkdir /usr/local/stata10<br />cd /usr/local/stata10<br />sh /media/cdrom/install<br />Dont forget to give permissions to read and write and create and delete files to this folder. <br /><code class="functions">sudo chmod -R 755 /usr/local/stata10</code><code class="plain"></code><br />Alternatively, use gksudo nautilus and create the required folder /usr/local/stata10. Make a right click over stata10 folder and choose properties, and give all the permissions. No other folder is allowed. <br />After you choose the required options, in Ubuntu first 3. Linux then the version of Stata you have, either Linux 32 or Linux 64. <br />You&#8217;re not finished yet, you need to make the symbolic link necessary to run the Stata gui:<br />sudo ln -s /usr/lib64/x86_64-linux-gnu/libtiff.so.4.3.3 /usr/lib/libtiff.so.3<br />If, you&#8217;re running a different version of ubuntu you have to locate libtiff.so and make a symbolic link with the required version of Stata (if it already exists libtiff.so.4.3.3 or whatever version there is -i have not tested others but I know v 4 works). If the file doesn&#8217;t exist install it. (Synaptic)<br />Finally, you need the&nbsp; <b>libgtksourceviewmm-1.0-dev through Synaptic or sudo apt-get it!<br /></b>In the /usr/local/stata10 folder run <br />./xstata you have the gui, and then run update all<br />If you want the console<br />./stata<br />Finally, you can install ess emacs speaks statistics from the repositories and emacs will help you edit your scripts. <br />Enjoy, Stata under ubuntu. </p>
<p><b><br /></b></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vhaguiar.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vhaguiar.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vhaguiar.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vhaguiar.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vhaguiar.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vhaguiar.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vhaguiar.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vhaguiar.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vhaguiar.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vhaguiar.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vhaguiar.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vhaguiar.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vhaguiar.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vhaguiar.wordpress.com/126/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vhaguiar.wordpress.com&amp;blog=7156458&amp;post=126&amp;subd=vhaguiar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vhaguiar.wordpress.com/2011/07/31/stata-install-stata-10-mp-dynamically-linked-gui-in-ubuntu-natty/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7b3dce594765a38d0aad4c98c9f342a1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vhaguiar</media:title>
		</media:content>
	</item>
		<item>
		<title>python: install openopt</title>
		<link>http://vhaguiar.wordpress.com/2011/07/22/python-install-openopt/</link>
		<comments>http://vhaguiar.wordpress.com/2011/07/22/python-install-openopt/#comments</comments>
		<pubDate>Fri, 22 Jul 2011 04:30:14 +0000</pubDate>
		<dc:creator>vhaguiar</dc:creator>
				<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">https://vhaguiar.wordpress.com/2011/07/22/python-install-openopt/</guid>
		<description><![CDATA[1. Download MinGW322. Add to the PATH &#8220;C:\Mingw32\bin&#8221; 3. Download openopt package from the author&#8217;s web4. open a prompt and cd the directory where the files had been uncompressed 5. execute in the openopt foldersetup.py config &#8211;compiler=mingw32 build &#8211;compiler=mingw32<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vhaguiar.wordpress.com&amp;blog=7156458&amp;post=124&amp;subd=vhaguiar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>1. Download MinGW32<br />2. Add to the PATH &#8220;C:\Mingw32\bin&#8221; <br />3. Download openopt package from the author&#8217;s web<br />4. open a prompt and cd the directory where the files had been uncompressed <br />5. execute in the openopt folder<br />setup.py config &#8211;compiler=mingw32 build &#8211;compiler=mingw32</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vhaguiar.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vhaguiar.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vhaguiar.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vhaguiar.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vhaguiar.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vhaguiar.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vhaguiar.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vhaguiar.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vhaguiar.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vhaguiar.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vhaguiar.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vhaguiar.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vhaguiar.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vhaguiar.wordpress.com/124/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vhaguiar.wordpress.com&amp;blog=7156458&amp;post=124&amp;subd=vhaguiar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vhaguiar.wordpress.com/2011/07/22/python-install-openopt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7b3dce594765a38d0aad4c98c9f342a1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vhaguiar</media:title>
		</media:content>
	</item>
		<item>
		<title>R: Installing nloptr in R 2.13 under Windows 7.</title>
		<link>http://vhaguiar.wordpress.com/2011/07/03/r-installing-nloptr-in-r-2-13-under-windows-7/</link>
		<comments>http://vhaguiar.wordpress.com/2011/07/03/r-installing-nloptr-in-r-2-13-under-windows-7/#comments</comments>
		<pubDate>Sun, 03 Jul 2011 22:09:54 +0000</pubDate>
		<dc:creator>vhaguiar</dc:creator>
				<category><![CDATA[R]]></category>

		<guid isPermaLink="false">https://vhaguiar.wordpress.com/2011/07/03/r-installing-nloptr-in-r-2-13-under-windows-7/</guid>
		<description><![CDATA[This assumes you have installed Rtools and that R can compile packages from source.1. Download the source files nloptr-src,from the author&#8217;s web. Unzip this in any file for example C:\nloptr.2. Download the binaries from&#160; NLopt that correspond to the lastest version. This link refers to nlopt-2.2.3-dll.zip.3. Copy the whole content of nlopt-2.2.3-dll.zip. in C:\nloptr\src\nlopt-2.24. In [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vhaguiar.wordpress.com&amp;blog=7156458&amp;post=122&amp;subd=vhaguiar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This assumes you have installed Rtools and that R can compile packages from source.<br />1. Download the source files<a href="http://www.ucl.ac.uk/%7Euctpjyy/downloads/nloptr-src_0.8.1.zip"> nloptr-src,</a><br />from the author&#8217;s web. Unzip this in any file for example C:\nloptr.<br />2. Download the binaries from&nbsp;<a href="http://www.ucl.ac.uk/%7Euctpjyy/downloads/nloptr-src_0.8.1.zip"><a href="http://ab-initio.mit.edu/nlopt/nlopt-2.2.3-dll.zip"> </a>NLopt </a><br />that correspond to the lastest version. This link refers to nlopt-2.2.3-dll.zip.<br />3. Copy the whole content of nlopt-2.2.3-dll.zip. in C:\nloptr\src\nlopt-2.2<br />4. In R: run <br />install.packages(&#8220;C:/nloptr&#8221;,repos=NULL,type=&#8221;source&#8221;)<br />You will receive a warning that the file libnlopt-0.dll is not installed in your system, don&#8217;t worry and do the following step.<br />5. A new folder will be created with the name src-i386<br />Copy the file  C:\nloptr\src\nlopt-2.2\libnlopt-0.dll into   C:\nloptr\<b>src-i386</b>\nlopt-2.2\libnlopt-0.dll. <br />6. In R: run <br />
install.packages(&#8220;C:/nloptr&#8221;,repos=NULL,type=&#8221;source&#8221;)<br />7. It&#8217;s a messy intallations but it works fine. If you have better options please let me know. </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vhaguiar.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vhaguiar.wordpress.com/122/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vhaguiar.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vhaguiar.wordpress.com/122/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vhaguiar.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vhaguiar.wordpress.com/122/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vhaguiar.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vhaguiar.wordpress.com/122/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vhaguiar.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vhaguiar.wordpress.com/122/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vhaguiar.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vhaguiar.wordpress.com/122/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vhaguiar.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vhaguiar.wordpress.com/122/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vhaguiar.wordpress.com&amp;blog=7156458&amp;post=122&amp;subd=vhaguiar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vhaguiar.wordpress.com/2011/07/03/r-installing-nloptr-in-r-2-13-under-windows-7/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7b3dce594765a38d0aad4c98c9f342a1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vhaguiar</media:title>
		</media:content>
	</item>
		<item>
		<title>LaTex and Lyx: Excel to LaTex xl2Latex</title>
		<link>http://vhaguiar.wordpress.com/2011/06/22/latex-and-lyx-excel-to-latex-xl2latex/</link>
		<comments>http://vhaguiar.wordpress.com/2011/06/22/latex-and-lyx-excel-to-latex-xl2latex/#comments</comments>
		<pubDate>Wed, 22 Jun 2011 05:05:29 +0000</pubDate>
		<dc:creator>vhaguiar</dc:creator>
				<category><![CDATA[LaTex]]></category>

		<guid isPermaLink="false">https://vhaguiar.wordpress.com/2011/06/22/latex-and-lyx-excel-to-latex-xl2latex/</guid>
		<description><![CDATA[Excel to LaTex, xl2Latex can be downloaded from the web, it´s a .xla that enables a excel complement that converts an excel table that is selected with the mouse to a LaTex code, that can be copied in the clipboard or saved in a file.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vhaguiar.wordpress.com&amp;blog=7156458&amp;post=120&amp;subd=vhaguiar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Excel to LaTex, xl2Latex can be downloaded from the web, it´s a .xla that enables a excel complement that converts an excel table that is selected with the mouse to a LaTex code, that can be copied in the clipboard or saved in a file.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vhaguiar.wordpress.com/120/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vhaguiar.wordpress.com/120/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vhaguiar.wordpress.com/120/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vhaguiar.wordpress.com/120/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vhaguiar.wordpress.com/120/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vhaguiar.wordpress.com/120/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vhaguiar.wordpress.com/120/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vhaguiar.wordpress.com/120/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vhaguiar.wordpress.com/120/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vhaguiar.wordpress.com/120/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vhaguiar.wordpress.com/120/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vhaguiar.wordpress.com/120/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vhaguiar.wordpress.com/120/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vhaguiar.wordpress.com/120/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vhaguiar.wordpress.com&amp;blog=7156458&amp;post=120&amp;subd=vhaguiar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vhaguiar.wordpress.com/2011/06/22/latex-and-lyx-excel-to-latex-xl2latex/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7b3dce594765a38d0aad4c98c9f342a1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vhaguiar</media:title>
		</media:content>
	</item>
		<item>
		<title>R: How to install RMySQL in Windows 7 with MySQL 5.5</title>
		<link>http://vhaguiar.wordpress.com/2011/06/06/r-how-to-install-rmysql-in-windows-7-with-mysql-5-5/</link>
		<comments>http://vhaguiar.wordpress.com/2011/06/06/r-how-to-install-rmysql-in-windows-7-with-mysql-5-5/#comments</comments>
		<pubDate>Mon, 06 Jun 2011 03:00:14 +0000</pubDate>
		<dc:creator>vhaguiar</dc:creator>
				<category><![CDATA[R]]></category>

		<guid isPermaLink="false">https://vhaguiar.wordpress.com/2011/06/06/r-how-to-install-rmysql-in-windows-7-with-mysql-5-5/</guid>
		<description><![CDATA[This post is based completely (except for the bold characters) in the thread in http://stackoverflow.com/questions/5223113/using-mysql-in-r-for-windows, about how to install RMySQL in Windows 7 with MySQL 5.5 The answer is due to: Yuriy Petrovskiy &#8220;Found solution with help of ran2, who gave me link to common question. The basic process is described here, but there are [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vhaguiar.wordpress.com&amp;blog=7156458&amp;post=118&amp;subd=vhaguiar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This post is based completely (except for the bold characters) in the thread in http://stackoverflow.com/questions/5223113/using-mysql-in-r-for-windows,<br />
about how to install RMySQL in Windows 7 with MySQL 5.5<br />
The answer is due to:<br />
Yuriy Petrovskiy</p>
<p>&#8220;Found solution with help of ran2, who gave me link to common question. The basic process is described here, but there are several hints, So I will describe the whole solution (please change the R version and paths if needed):</p>
<p>1.    Install latest RTools from here<br />
2.    install MySQL or header and library files of mysql<br />
3.    create or edit file C:\Program Files\R\R-2.12.1\etc\Renviron.site and add line like MYSQL_HOME=C:/mysql (path to your mysql files)<br />
4. copy libmysql.lib from mysql/lib to mysql/lib/opt to meet dependencies.<br />
5. copy libmysql.dll to <b>C:\Program Files\R\R-2.13.0\bin\i386 </b>or to windows/system32 directory.<br />
6. run install.packages(&#8216;RMySQL&#8217;,type=&#8217;source&#8217;) and wait while compilation will end.<br />
&#8220;<br />
Hope it helps.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vhaguiar.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vhaguiar.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vhaguiar.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vhaguiar.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vhaguiar.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vhaguiar.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vhaguiar.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vhaguiar.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vhaguiar.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vhaguiar.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vhaguiar.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vhaguiar.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vhaguiar.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vhaguiar.wordpress.com/118/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vhaguiar.wordpress.com&amp;blog=7156458&amp;post=118&amp;subd=vhaguiar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vhaguiar.wordpress.com/2011/06/06/r-how-to-install-rmysql-in-windows-7-with-mysql-5-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7b3dce594765a38d0aad4c98c9f342a1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vhaguiar</media:title>
		</media:content>
	</item>
		<item>
		<title>Stata: An easy way to recode a variable using a dictionary.</title>
		<link>http://vhaguiar.wordpress.com/2011/05/21/stata-an-easy-way-to-recode-a-variable-using-a-dictionary/</link>
		<comments>http://vhaguiar.wordpress.com/2011/05/21/stata-an-easy-way-to-recode-a-variable-using-a-dictionary/#comments</comments>
		<pubDate>Sat, 21 May 2011 17:06:07 +0000</pubDate>
		<dc:creator>vhaguiar</dc:creator>
				<category><![CDATA[Stata]]></category>

		<guid isPermaLink="false">https://vhaguiar.wordpress.com/2011/05/21/stata-an-easy-way-to-recode-a-variable-using-a-dictionary/</guid>
		<description><![CDATA[The task, recode or create a new variable with a new codification from an original one. In this example we have ciiu industrial code to a user more aggregated groups in string. Remember that the dictionary must have a one to one correspondence, that is newcode originalcode &#8220;sector1&#8243; 1010 &#8220;sector1&#8243; 1020 // The first step [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vhaguiar.wordpress.com&amp;blog=7156458&amp;post=111&amp;subd=vhaguiar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The task, recode or create a new variable with a new codification from an original one. In this example we have ciiu industrial code to a user more aggregated groups in string.<br />
Remember that the dictionary must have a one to one correspondence, that is<br />
newcode originalcode<br />
&#8220;sector1&#8243; 1010<br />
&#8220;sector1&#8243; 1020</p>
<p>// The first step is to import the dictionary file:</p>
<p>insheet using &#8220;dict.txt&#8221;, tab<br />
// Next, make the stata file into a matrix, if you need string codes use names, for example rownames.<br />
mkmat originalcode, matrix(dict) rownames(stringvariable_newcode)</p>
<p>// Then use the database to be recoded. Do not use clear all because it will delete the matrix.<br />
use &#8220;database.dta&#8221;, clear</p>
<p>// Identify the original variable to be recoded.<br />
// Then use this code.<br />
// drop if exists the variable where the new codes are going to be written.<br />
capture drop recodmip<br />
gen recodmip=&#8221;"<br />
// calculates the row number of the matrix or the number of new codes.</p>
<p>local nrow=rowsof(dict)<br />
forval i=1(1)`nrow&#8217; {<br />
mat b=dict[`i',"originalcode"]<br />
local nam : rownames b<br />
di &#8220;`nam&#8217;&#8221; // the new code in string<br />
local codigo=b[1,1] // makes the matrix into a scalar.<br />
capture replace recodmip=&#8221;`nam&#8217;&#8221; if originalcode==`codigo&#8217;<br />
}</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vhaguiar.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vhaguiar.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vhaguiar.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vhaguiar.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vhaguiar.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vhaguiar.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vhaguiar.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vhaguiar.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vhaguiar.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vhaguiar.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vhaguiar.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vhaguiar.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vhaguiar.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vhaguiar.wordpress.com/111/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vhaguiar.wordpress.com&amp;blog=7156458&amp;post=111&amp;subd=vhaguiar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vhaguiar.wordpress.com/2011/05/21/stata-an-easy-way-to-recode-a-variable-using-a-dictionary/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7b3dce594765a38d0aad4c98c9f342a1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vhaguiar</media:title>
		</media:content>
	</item>
		<item>
		<title>R: gsub replacing a dot &#8220;.&#8221;</title>
		<link>http://vhaguiar.wordpress.com/2011/03/01/r-gsub-replacing-a-dot/</link>
		<comments>http://vhaguiar.wordpress.com/2011/03/01/r-gsub-replacing-a-dot/#comments</comments>
		<pubDate>Tue, 01 Mar 2011 19:45:18 +0000</pubDate>
		<dc:creator>vhaguiar</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://vhaguiar.wordpress.com/2011/03/01/r-gsub-replacing-a-dot/</guid>
		<description><![CDATA[The gsub command in R v. 2.12.1 when applies the gsub(pattern=&#8221;.&#8221;,replacement=&#8221;",text) recognizes &#8220;.&#8221; as a wild card for replace all, to solve this you must use gsub(pattern=&#8221;\\.&#8221;,replacement=&#8221;",text), to replace only the dots.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vhaguiar.wordpress.com&amp;blog=7156458&amp;post=104&amp;subd=vhaguiar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The gsub command in R v. 2.12.1 when applies the gsub(pattern=&#8221;.&#8221;,replacement=&#8221;",text) recognizes &#8220;.&#8221; as a wild card for replace all, <br />to solve this you must use gsub(pattern=&#8221;\\.&#8221;,replacement=&#8221;",text), to replace only the dots. </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vhaguiar.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vhaguiar.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vhaguiar.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vhaguiar.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vhaguiar.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vhaguiar.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vhaguiar.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vhaguiar.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vhaguiar.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vhaguiar.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vhaguiar.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vhaguiar.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vhaguiar.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vhaguiar.wordpress.com/104/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vhaguiar.wordpress.com&amp;blog=7156458&amp;post=104&amp;subd=vhaguiar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vhaguiar.wordpress.com/2011/03/01/r-gsub-replacing-a-dot/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7b3dce594765a38d0aad4c98c9f342a1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vhaguiar</media:title>
		</media:content>
	</item>
		<item>
		<title>Python: Rpy2 with Python 2.6 and R 2.12.1 R_HOME path problem.</title>
		<link>http://vhaguiar.wordpress.com/2011/03/01/python-rpy2-with-python-2-6-and-r-2-12-1-r_home-path-problem-2/</link>
		<comments>http://vhaguiar.wordpress.com/2011/03/01/python-rpy2-with-python-2-6-and-r-2-12-1-r_home-path-problem-2/#comments</comments>
		<pubDate>Tue, 01 Mar 2011 01:15:28 +0000</pubDate>
		<dc:creator>vhaguiar</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://vhaguiar.wordpress.com/2011/03/01/python-rpy2-with-python-2-6-and-r-2-12-1-r_home-path-problem-2/</guid>
		<description><![CDATA[Rpy2 is a great Python package, nonetheless, it has problems running with newer versions of R (for instance 2.12.1), because R comes with a new directory structure. Specifically, it includes both 32 bits and 64 bits versions in separated folders, and the former lib folder was renamed to library. To adjust for this changes, the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vhaguiar.wordpress.com&amp;blog=7156458&amp;post=102&amp;subd=vhaguiar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Rpy2 is a great Python package, nonetheless, it has problems running with newer versions of R (for instance 2.12.1), because R comes with a new directory structure. Specifically, it includes both 32 bits and 64 bits versions in separated folders, and the former lib folder was renamed to library. To adjust for this changes, the \rinterface\__init__.py script located under the folder of rpy2\rinterface has to be modified.<br />
The lines that were added have a #ADDED comment next to them. </p>
<p><pre class="brush: python;">

import os, sys

try:
    R_HOME = (os.environ[&amp;quot;R_HOME&amp;quot;], )
except KeyError:
    R_HOME = os.popen(&amp;quot;R RHOME&amp;quot;).readlines()

if len(R_HOME) == 0:
    if sys.platform == 'win32':
        try:
            import win32api
            import win32con
            hkey = win32api.RegOpenKeyEx(win32con.HKEY_LOCAL_MACHINE,
                                         &amp;quot;Software\\R-core\\R&amp;quot;,
                                         0, win32con.KEY_QUERY_VALUE )
            R_HOME = win32api.RegQueryValueEx(hkey, &amp;quot;InstallPath&amp;quot;)[0]
            win32api.RegCloseKey( hkey )
        except:
            raise RuntimeError(
                &amp;quot;Unable to determine R version from the registery.&amp;quot; +\
                &amp;quot;Calling the command 'R RHOME' does not return anything.\n&amp;quot; +\
                    &amp;quot;This might be because R.exe is nowhere in your Path.&amp;quot;)
    else:
        raise RuntimeError(
            &amp;quot;R_HOME not defined, and no R command in the PATH.&amp;quot;
            )
else:
#Twist if 'R RHOME' spits out a warning
    if R_HOME[0].startswith(&amp;quot;WARNING&amp;quot;):
        R_HOME = R_HOME[1]
    else:
        R_HOME = R_HOME[0]
        R_HOME = R_HOME.strip()

os.environ['R_HOME'] = R_HOME

# Win32-specific code copied from RPy-1.x
if sys.platform == 'win32':
    import win32api
    #os.environ['PATH'] += ';' + os.path.join(R_HOME, 'bin')
    #os.environ['PATH'] += ';' + os.path.join(R_HOME, 'modules')
    #os.environ['PATH'] += ';' + os.path.join(R_HOME, 'lib') 
    os.environ['PATH'] += ';' + os.path.join(R_HOME, 'bin', 'i386') #ADD
    os.environ['PATH'] += ';' + os.path.join(R_HOME, 'modules', 'i386') #ADD
    os.environ['PATH'] += ';' + os.path.join(R_HOME, 'library') #ADD

    # Load the R dll using the explicit path
    # First try the bin dir:
    Rlib = os.path.join(R_HOME, 'bin', 'R.dll')
	
    # Then the lib dir:
    #if not os.path.exists(Rlib):
    #    Rlib = os.path.join(R_HOME, 'lib', 'R.dll')
    
	# Try bin/i386 subdirectory seen in R 2.12.0  ## ADDED ##
    if not os.path.exists(Rlib):                ## ADDED ##
        Rlib = os.path.join(R_HOME, 'bin', 'i386', 'R.dll')  ## ADDED ##
    
	# Otherwise fail out!
    if not os.path.exists(Rlib):
        raise RuntimeError(&amp;quot;Unable to locate R.dll within %s&amp;quot; % R_HOME)

    win32api.LoadLibrary( Rlib )


# cleanup the namespace
del(os)
try:
    del(win32api)
    del(win32con)
except:
    pass


from rpy2.rinterface.rinterface import *

class StrSexpVector(SexpVector):
    &amp;quot;&amp;quot;&amp;quot; 
    Vector of strings.
    &amp;quot;&amp;quot;&amp;quot;
    def __init__(self, v):
        super(StrSexpVector, self).__init__(v, STRSXP)


class IntSexpVector(SexpVector):
    &amp;quot;&amp;quot;&amp;quot; 
    Vector of integers.
    &amp;quot;&amp;quot;&amp;quot;
    def __init__(self, v):        
        super(IntSexpVector, self).__init__(v, INTSXP)


class FloatSexpVector(SexpVector):
    &amp;quot;&amp;quot;&amp;quot; 
    Vector of floats.
    &amp;quot;&amp;quot;&amp;quot;
    def __init__(self, v):        
        super(FloatSexpVector, self).__init__(v, REALSXP)

class BoolSexpVector(SexpVector):
    &amp;quot;&amp;quot;&amp;quot; 
    Vector of booleans (logical in R terminology).
    &amp;quot;&amp;quot;&amp;quot;
    def __init__(self, v):        
        super(BoolSexpVector, self).__init__(v, LGLSXP)


# wrapper because print is strangely not a function
# Python prior to version 3.0
def consolePrint(x):
    &amp;quot;&amp;quot;&amp;quot;This is the default callback for R's console. It simply writes to stdout.&amp;quot;&amp;quot;&amp;quot;
    sys.stdout.write(x)

setWriteConsole(consolePrint)

def consoleFlush():
    sys.stdout.flush()

setFlushConsole(consoleFlush)


def consoleRead(prompt):
    input = raw_input(prompt)
    input += &amp;quot;\n&amp;quot;
    return input

setReadConsole(consoleRead)




</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vhaguiar.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vhaguiar.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vhaguiar.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vhaguiar.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vhaguiar.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vhaguiar.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vhaguiar.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vhaguiar.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vhaguiar.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vhaguiar.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vhaguiar.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vhaguiar.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vhaguiar.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vhaguiar.wordpress.com/102/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vhaguiar.wordpress.com&amp;blog=7156458&amp;post=102&amp;subd=vhaguiar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vhaguiar.wordpress.com/2011/03/01/python-rpy2-with-python-2-6-and-r-2-12-1-r_home-path-problem-2/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7b3dce594765a38d0aad4c98c9f342a1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vhaguiar</media:title>
		</media:content>
	</item>
	</channel>
</rss>
