Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Error: Undefined function or variable 'x'

Subject: Error: Undefined function or variable 'x'

From: Anil

Date: 19 May, 2011 16:53:02

Message: 1 of 6

Hello,
I am no Matlab programmer but trying to run a function that someone else
wrote. The command to run the function is: panel_effects_sdm(results,Vnames3,
W);
I am geeting the error:
??? Undefined function or variable 'x'.

Error in ==> panel_effects_sdm at 56
   tmp = [x W*y]'*[x W*y];
Here is the function:

ndraw=1000;
uiter=50;
maxorderu=100;
nobs = results.N;
rv=randn(nobs,uiter);
tracew=zeros(maxorderu,1);
wjjju=rv;
for jjj=1:maxorderu
   wjjju=W*wjjju;
   tracew(jjj)=mean(mean(rv.*wjjju));
   
end

traces=[tracew];
traces(1)=0;
traces(2)=sum(sum(W'.*W))/nobs;
trs=[1;traces];
ntrs=length(trs);
trbig=trs';
trbig2 = [trbig(1,2:end) trbig(1,end)];
trmat = [trbig
        trbig2];
    
% cheat here to fix the numerical hessian
% Use MCMC to get good results
sigmat = results.cov - diag(diag(results.cov)) + diag(diag(abs(results.cov)))
;
sigmatt = sigmat(1:end-1,1:end-1);
[R,posdef] = chol(sigmatt);

if posdef ~= 0 % even cheating did not work, so punt with a kludge
   tmp = [x W*y]'*[x W*y];
   sigmatt = sige*(inv(tmp));
end;

tmp = [results.beta
   results.rho];

bdraws = matadd(norm_rndmat(sigmatt,ndraw),tmp);
draws = bdraws';

psave = draws(:,end);
ind = find(psave > 1); % find bad rho draws
psave(ind,1) = 0.99; % replace them with 0.99

bsave = draws(:,1:end-1);
[junk nvartot]=size(results.xwith);

if results.cflag == 1
   bdraws = bsave(:,2:end);
   nvar = (nvartot-1)/2;
elseif results.cflag == 0
   bdraws = bsave;
   nvar = nvartot/2;
end;
pdraws = psave;

ree = 0:1:ntrs-1;

rmat = zeros(1,ntrs);
total = zeros(ndraw,nvar,ntrs);
direct = zeros(ndraw,nvar,ntrs);
indirect = zeros(ndraw,nvar,ntrs);

for i=1:ndraw;
   rmat = pdraws(i,1).^ree;
   for j=1:nvar;
       beta = [bdraws(i,j) bdraws(i,j+nvar)];
       total(i,j,:) = (beta(1,1) + beta(1,2))*rmat;
       direct(i,j,:) = (beta*trmat).*rmat;
       indirect(i,j,:) = total(i,j,:) - direct(i,j,:);
   end;
   
end;

% Compute means, std deviation and upper and lower 0.95 intervals
p = nvar;
total_out = zeros(p,5);
total_save = zeros(ndraw,p);
for i=1:p;
   tmp = squeeze(total(:,i,:)); % an ndraw by 1 by ntraces matrix
   total_mean = mean(tmp);
   total_std = std(tmp);
   % Bayesian 0.95 credible intervals
   % for the cumulative total effects
   total_sum = (sum(tmp'))'; % an ndraw by 1 vector
   cum_mean = cumsum(mean(tmp));
   cum_std = cumsum(std(tmp));
   total_save(:,i) = total_sum;
   bounds = cr_interval(total_sum,0.95);
   cmean = mean(total_sum);
   smean = std(total_sum);
   ubounds = bounds(1,1);
   lbounds = bounds(1,2);
   total_out(i,:) = [cmean cmean./smean tdis_prb(cmean./smean,nobs) lbounds
ubounds];
end;

% now do indirect effects
indirect_out = zeros(p,5);
indirect_save = zeros(ndraw,p);
for i=1:p;
   tmp = squeeze(indirect(:,i,:)); % an ndraw by 1 by ntraces matrix
   indirect_mean = mean(tmp);
   indirect_std = std(tmp);
   % Bayesian 0.95 credible intervals
   % for the cumulative indirect effects
   indirect_sum = (sum(tmp'))'; % an ndraw by 1 vector
   cum_mean = cumsum(mean(tmp));
   cum_std = cumsum(std(tmp));
   indirect_save(:,i) = indirect_sum;
   bounds = cr_interval(indirect_sum,0.95);
   cmean = mean(indirect_sum);
   smean = std(indirect_sum);
   ubounds = bounds(1,1);
   lbounds = bounds(1,2);
   indirect_out(i,:) = [cmean cmean./smean tdis_prb(cmean./smean,nobs)
lbounds ubounds];
end;

% now do direct effects
direct_out = zeros(p,5);
direct_save = zeros(ndraw,p);
for i=1:p;
   tmp = squeeze(direct(:,i,:)); % an ndraw by 1 by ntraces matrix
   direct_mean = mean(tmp);
   direct_std = std(tmp);
   % Bayesian 0.95 credible intervals
   % for the cumulative direct effects
   direct_sum = (sum(tmp'))'; % an ndraw by 1 vector
   cum_mean = cumsum(mean(tmp));
   cum_std = cumsum(std(tmp));
   direct_save(:,i) = direct_sum;
   bounds = cr_interval(direct_sum,0.95);
   cmean = mean(direct_sum);
   smean = std(direct_sum);
   ubounds = bounds(1,1);
   lbounds = bounds(1,2);
   direct_out(i,:) = [cmean cmean./smean tdis_prb(cmean./smean,nobs) lbounds
ubounds];
end;

% now print x-effects estimates

bstring = 'Coefficient';
tstring = 't-stat';
pstring = 't-prob';
lstring = 'lower 05';
ustring = 'upper 95';
cnames = strvcat(bstring,tstring,pstring,lstring,ustring);
ini.cnames = cnames;
ini.width = 2000;

% print effects estimates
if results.cflag == 1
   vnameso = strvcat(vnames(3:2+nvar,:));
elseif results.cflag == 0
   vnameso = strvcat(vnames(2:1+nvar,:));
end;

ini.rnames = strvcat('Direct ',vnameso);
ini.fmt = '%16.6f';
ini.fid = 1;

% set up print out matrix
printout = direct_out;
mprint(printout,ini);

printout = indirect_out;
ini.rnames = strvcat('Indirect',vnameso);
mprint(printout,ini);

printout = total_out;
ini.rnames = strvcat('Total ',vnameso);
mprint(printout,ini);

Subject: Error: Undefined function or variable 'x'

From: dpb

Date: 19 May, 2011 17:01:46

Message: 2 of 6

On 5/19/2011 11:53 AM, Anil wrote:
> Hello, I am no Matlab programmer but trying to run a function that
> someone else wrote. The command to run the function is:
> panel_effects_sdm(results,Vnames3,W);
> I am geeting the error:
> ??? Undefined function or variable 'x'.
>
> Error in ==> panel_effects_sdm at 56
> tmp = [x W*y]'*[x W*y];
> Here is the function:
>
> ndraw=1000;
> uiter=50;
...

...[code elided for brevity; it's immaterial]...

That isn't a function, it's a script(1).

It expects everything to be in the workspace, not passed as arguments.

(1) The giveaway is there's no function definition line at the beginning.

--

Subject: Error: Undefined function or variable 'x'

From: Anil

Date: 19 May, 2011 17:18:05

Message: 3 of 6

Ok. This is what is at the beginning of the script:

function panel_effects_sdm(results,vnames,W)
% PURPOSE: Computes and prints direct, indirect and total effects estimates
% for Elhorst SDM spatial panel models using the LeSage and Pace code
%---------------------------------------------------
% USAGE: panel_effects_sdm(results,vnames,W)
% Where: results = a structure returned by a spatial panel regression
% vnames = a structure of variable names
% W = spatial weights matrix used to estimate model
%---------------------------------------------------

dpb <none@non.net> wrote in message <ir3idn$v48$1@speranza.aioe.org>...
> On 5/19/2011 11:53 AM, Anil wrote:
> > Hello, I am no Matlab programmer but trying to run a function that
> > someone else wrote. The command to run the function is:
> > panel_effects_sdm(results,Vnames3,W);
> > I am geeting the error:
> > ??? Undefined function or variable 'x'.
> >
> > Error in ==> panel_effects_sdm at 56
> > tmp = [x W*y]'*[x W*y];
> > Here is the function:
> >
> > ndraw=1000;
> > uiter=50;
> ...
>
> ...[code elided for brevity; it's immaterial]...
>
> That isn't a function, it's a script(1).
>
> It expects everything to be in the workspace, not passed as arguments.
>
> (1) The giveaway is there's no function definition line at the beginning.
>
> --

Subject: Error: Undefined function or variable 'x'

From: Florin Neacsu

Date: 19 May, 2011 17:51:05

Message: 4 of 6

"Anil" wrote in message <ir3jcd$27s$1@newscl01ah.mathworks.com>...
> Ok. This is what is at the beginning of the script:
>
> function panel_effects_sdm(results,vnames,W)
> % PURPOSE: Computes and prints direct, indirect and total effects estimates
> % for Elhorst SDM spatial panel models using the LeSage and Pace code
> %---------------------------------------------------
> % USAGE: panel_effects_sdm(results,vnames,W)
> % Where: results = a structure returned by a spatial panel regression
> % vnames = a structure of variable names
> % W = spatial weights matrix used to estimate model
> %---------------------------------------------------
>
> dpb <none@non.net> wrote in message <ir3idn$v48$1@speranza.aioe.org>...
> > On 5/19/2011 11:53 AM, Anil wrote:
> > > Hello, I am no Matlab programmer but trying to run a function that
> > > someone else wrote. The command to run the function is:
> > > panel_effects_sdm(results,Vnames3,W);
> > > I am geeting the error:
> > > ??? Undefined function or variable 'x'.
> > >
> > > Error in ==> panel_effects_sdm at 56
> > > tmp = [x W*y]'*[x W*y];
> > > Here is the function:
> > >
> > > ndraw=1000;
> > > uiter=50;
> > ...
> >
> > ...[code elided for brevity; it's immaterial]...
> >
> > That isn't a function, it's a script(1).
> >
> > It expects everything to be in the workspace, not passed as arguments.
> >
> > (1) The giveaway is there's no function definition line at the beginning.
> >
> > --

Hi,

The error is pretty much self-explanatory
??? Undefined function or variable 'x'.

Error in ==> panel_effects_sdm at 56
   tmp = [x W*y]'*[x W*y];

when you try to make temp you need a variable x that appears for the first time in your function. If I understand correctly you did not write this function, but you if you can understand what it does than you should be able to fix it. Maybe there is just a line missing where x is assigned. Anyway, you can probably fix it faster by asking the person who wrote it than here. It is hard/time consuming to go through a long program and figure out what x is supposed to be.

Regards,
Florin

Subject: Error: Undefined function or variable 'x'

From: dpb

Date: 19 May, 2011 18:19:27

Message: 5 of 6

On 5/19/2011 12:18 PM, Anil wrote:
...[top posting repaired--don't do that; hard conversation follow make]...
> dpb <none@non.net> wrote in message <ir3idn$v48$1@speranza.aioe.org>...
>...
>> ...[code elided for brevity; it's immaterial]...
>>
>> That isn't a function, it's a script(1).
>>
>> It expects everything to be in the workspace, not passed as arguments.
>>
>> (1) The giveaway is there's no function definition line at the beginning.
...
 > Ok. This is what is at the beginning of the script:
 >
 > function panel_effects_sdm(results,vnames,W)
 > % PURPOSE: Computes and prints direct, indirect and total effects
estimates
 > % for Elhorst SDM spatial panel models using the LeSage and Pace code
 > %---------------------------------------------------
 > % USAGE: panel_effects_sdm(results,vnames,W)
 > % Where: results = a structure returned by a spatial panel regression
 > % vnames = a structure of variable names
 > % W = spatial weights matrix used to estimate model
 > %---------------------------------------------------
 >

OK, then it _is_ a function after all. See what happens when you ask a
question but don't provide the necessary input?

Well, looks like you're in deep trouble here...

 From your previous post here's the section that aborted; read the
comment and weep...
...
[R,posdef] = chol(sigmatt);

if posdef ~= 0 % even cheating did not work, so punt with a kludge
   tmp = [x W*y]'*[x W*y];
   sigmatt = sige*(inv(tmp));
end;

The code discovered that the matrix being operated on isn't positive
definite (and apparently by the comment even that was a fixup attempt).

Unfortunately, it looks like the author must have never had to use or
test the kludge as there doesn't seem to be any 'x' having been defined
to be used at that point.

Your mission, should you choose to accept it, is to figure out what your
input _should_ have been to the function to have prevented this piece of
code from being executed. Failing that, I guess you could try to divine
what the author really meant for x to be.

Either way, methinks you're in trouble here and the question doesn't
really have anything to do w/ Matlab per se...

--

Subject: Error: Undefined function or variable 'x'

From: Anil

Date: 19 May, 2011 19:25:21

Message: 6 of 6

Dear dpb,
Thank you!!!
It is kind of depressing to read your posting but it did help! And that is all that matters!
Yes, the matrix was not positive definite and I fixed it and ran the function and it worked and I have the results.
Thanks again
Anil


dpb <none@non.net> wrote in message <ir3mvc$cbl$1@speranza.aioe.org>...
> On 5/19/2011 12:18 PM, Anil wrote:
> ...[top posting repaired--don't do that; hard conversation follow make]...
> > dpb <none@non.net> wrote in message <ir3idn$v48$1@speranza.aioe.org>...
> >...
> >> ...[code elided for brevity; it's immaterial]...
> >>
> >> That isn't a function, it's a script(1).
> >>
> >> It expects everything to be in the workspace, not passed as arguments.
> >>
> >> (1) The giveaway is there's no function definition line at the beginning.
> ...
> > Ok. This is what is at the beginning of the script:
> >
> > function panel_effects_sdm(results,vnames,W)
> > % PURPOSE: Computes and prints direct, indirect and total effects
> estimates
> > % for Elhorst SDM spatial panel models using the LeSage and Pace code
> > %---------------------------------------------------
> > % USAGE: panel_effects_sdm(results,vnames,W)
> > % Where: results = a structure returned by a spatial panel regression
> > % vnames = a structure of variable names
> > % W = spatial weights matrix used to estimate model
> > %---------------------------------------------------
> >
>
> OK, then it _is_ a function after all. See what happens when you ask a
> question but don't provide the necessary input?
>
> Well, looks like you're in deep trouble here...
>
> From your previous post here's the section that aborted; read the
> comment and weep...
> ...
> [R,posdef] = chol(sigmatt);
>
> if posdef ~= 0 % even cheating did not work, so punt with a kludge
> tmp = [x W*y]'*[x W*y];
> sigmatt = sige*(inv(tmp));
> end;
>
> The code discovered that the matrix being operated on isn't positive
> definite (and apparently by the comment even that was a fixup attempt).
>
> Unfortunately, it looks like the author must have never had to use or
> test the kludge as there doesn't seem to be any 'x' having been defined
> to be used at that point.
>
> Your mission, should you choose to accept it, is to figure out what your
> input _should_ have been to the function to have prevented this piece of
> code from being executed. Failing that, I guess you could try to divine
> what the author really meant for x to be.
>
> Either way, methinks you're in trouble here and the question doesn't
> really have anything to do w/ Matlab per se...
>
> --

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us