Using stan's multi_normal distribution - linear-regression

I am trying to do a pooled Bayesian model of the type $\log(y)=\beta_1*\log(x_1)+\beta_2*\log(x_2)+\alpha$. I'm having problems sampling $\beta$ using stan's multi_normal distribution (I'm running it on rstan), and I get the following error message:
'Error in stanc(filename, allow_undefined = TRUE) : 0
Syntax error in 'string', line 14, column 29 to column 35, parsing error:
Ill-formed expression. Expected a comma-separated list of expressions.
I suspect there's some kind of syntax error when I run R's code checker, but I can't see the mistake. Below is the code. What I want in the end, is to get estimates for $\beta$ and $\alpha$.
data {
int<lower=0> N; //number of observations
int<lower=2> K; //number of explanatory variables
matrix[N,K] x; //explanatory variables
vector[N] y; //outcome
}
parameters {
vector[K] beta;
real alpha;
real sigma;
}
model {
//priors
beta ~ multi_normal(c(0,0),matrix(c(1,0.8,0.8,1),ncol=2));
alpha ~ normal(0,10^10);
sigma ~ uniform(0,100);
// likelihood
for (i in 1:N)
log(y)[i] ~ normal(log(x[i,1])*beta[1]+log(x[i,2])*beta[2] + alpha, sigma);
generated quantities {
vector[K] beta;
real alpha;
real sigma;
}

Related

Receiving MATLAB Error Regarding Function Arguments

When running my MATLAB script below, I keep getting an error that states:
Error using spa (Line 147)
The value of the window
size must be an integer greater than 2.
Error in "projectName" G = spa(xFunction2, x)
I've tried putting in multiple types of arguments into "spa" (data, windowsize, frequency) but it still yields the same error(s). Help?
n = 1:1024;
%Signal Function
xFunction = sqrt(10)*exp(j*2*pi*0.10*n)+ sqrt(20)*exp(j*2*pi*0.20*n) + sqrt(625);
%Complex Noise Function
zFunction = 0.707*randn(size(n)) + j*0.707*randn(size(n));
%Computing the value of x(n) + sqrt(625)*z
xFunction2 = xFunction + sqrt(625)*zFunction;
G = spa(xFunction2,51);
figure(1);
plot(w, 10*log10(G));
Acording the documentation of spa the first argument is iddata type. In addition, the time serie has to be a column vector.
So, to make it works change G = spa(xFunction2,51); for G = spa(xFunction2(:),51);. To do it the correct way, convert your time serie to iddata:
Ts = 0.1; % what ever is your sampling time.
myiddata = iddata(xFunction2(:),[],Ts);
G = spa(myiddata,51);
In addition, you should use spectrum(G) or bode(G)to plot the result.

Trying to implement Richardson's Extrapolation - Basic Syntax Assistance

I so far have the following
y = log(x);
% Ask user for input values for h and M
% M denotes the number of steps of the algorithm.
h = input('Input value h: ');
M = input('Input value M: ');
%Initialize an MxM matrix
D = zeros(M);
phi = (1/(2*h)) * (y(x+h) - y(x-h));
print(phi);
I obtain the error
Error using symengine (line 58) Index exceeds matrix dimensions.
Error in sym/subsref (line 696)
B = mupadmex('symobj::subsref',A.s,inds{:});
Error in RE (line 12) phi = (1/(2*h)) * (y(x+h) - y(x-h));
First, I believe I should be getting an error message about x not being defined. Second, I have no idea what the matrix dimension error is about. Third, and most importantly, how can I declare the function phi so that it becomes what I wrote?
First, I believe I should be getting an error message about x not being defined.
I'm guessing that x is defined, or you would get that error upon the line defining phi. To check whether x is defined, type "who" or "whos".
Second, I have no idea what the matrix dimension error is about.
This is most likely because y is a scalar, x + h is equal to some nonzero integer that is not 1, and you're trying to access y(x + h). For your own edification try setting y equal to a scalar (e.g. y = 5;) and seeing what errors are produced by indexing it in various legitimate and non-legitimate ways (e.g. y(1), y(0), y(3), y(-1), y(1.5)).
Third, and most importantly, how can I declare the function phi so that it becomes what I wrote?
Based on the context it looks like you want y to be defined as a function of x instead of a scalar. In other words:
y = #(x)log(x);
phi = (1/(2*h)) * (y(x+h) - y(x-h));
The code runs without error when you change the definitions to the above.
One other error you will run into: the print command is not what you're looking for - this prints a figure to a file. You're probably looking for:
disp(phi);

Matlab: mix-up of built-in and external functions

Matlab (2015a) is behaving weirdly: a number of builtin functions are not responding as expected. For instance, typing
ttest([1 2], [1 2])
results in
Error using size
Dimension argument must be a positive integer scalar within indexing range.
Error in nanstd (line 59)
tile(dim) = size(x,dim);
Error in ttest (line 132)
sdpop = nanstd(x,[],dim);
If I do a which for each of these functions:
which size
which nanstd
which ttest
I get, respetively:
built-in (C:\Program Files\MATLAB\R2015a\toolbox\matlab\elmat\size)
C:\Program Files\MATLAB\R2015a\toolbox\stats\eml\nanstd.m
C:\Program Files\MATLAB\R2015a\toolbox\stats\stats\ttest.m
Each of these files looks fine, except that size.m has each one of its rows commented out.
What could be the problem here?
Perhaps related to your problem:
ttest for R2013a makes the following call:
sdpop = nanstd(x,[],dim);
The helpfile for R2013a version of nanstd states:
Y = nanstd(X,FLAG,DIM) takes the standard deviation along dimension DIM of X.
On the other hand, nanstd in the 2005 nansuite package downloaded off Mathworks file exchange states:
FORMAT: Y = nanstd(X,DIM,FLAG)
Notice how DIM and FLAG are reversed!
If I call R2013a's ttest such that it makes a call to the old, 2005 nansuite function nanstd, Matlab generates an error similar to yours:
Error using size
Dimension argument must be a positive integer scalar within indexing range.
Error in nanmean (line 46)
count = size(x,dim) - sum(nans,dim);
Error in nanstd (line 54)
avg = nanmean(x,dim);
Error in ttest (line 132)
sdpop = nanstd(x,[],dim);
If [] is passed as DIM instead of FLAG, then nanstd's call to size(x, DIM) triggers an error because [] is not a positive integer scalar. If something like this is the cause, the broader question is, what's going on with your Matlab install or setup or downloads or whatever such that you're calling archaic code? Or why is that archaic code even around? I don't know at what point in Matlab's release history that nanstd(x, FLAG, DIM) became supported (instead of simply nanstd(x, DIM))?
Archive: below is my old answer which misdiagnosed your problem
Both of your sample vectors x and y are the same (i.e. [1,2]). The estimated variance of the difference is 0, and all your stats are going to blow up with NaN.
Do the stats step by step, and it will be clear what's going on.
x = [1; 2]; % Data you used in the example.
y = [1; 2]; % Data you used in the example.
z = x - y; % Your call to ttest tests whether this vector is different from zero at a statistically significant level.
Now we do all the stats on z
r.n = length(z);
r.mu = mean(z);
r.standard_error = sqrt(var(z,1) / (r.n-1)); % For your data, this will be zero since z is constant!
r.t = r.mu ./ r.standard_error; % For your data, this will be inf because dividing by zero!
r.df = r.n - 1;
r.pvals(r.t >= 0) = 2 * (1 - tcdf(r.t(r.t>=0), r.df)); % For your data, tcdf returns NaN and this all fails...
r.pvals(r.t < 0) = 2 * tcdf(r.t(r.t<0), r.df);
etc...
This should match a call to
[h, p, ci, stats] = ttest(x-y);

Subscripted assignment dimension mismatch for Matrix transpose

My motive here is to calculate parameters of a model using Least Square Estimation method.
Variable W stores the 'time' and CF stores the no. of faults occurred. I tried testing it on different Software Reliability Models (more like functions, as I've define below in 'func' variable), and only in some of them this error occurred.
W=xlsread(file,'A:A');
CF=xlsread(file,'B:B');
a(1)=100;
a(2)=1;
dy = CF*0.04;
func = inline('a(1).*(1-exp(-a(2).*W))','a','W');
Nrepeat=100;
sd = 0.3;
zfactor = 2;
outcut=10;
pList=zeros(Nrepeat,3);
for rep =1:Nrepeat
rep
[a,r,j] = nlinfit(W,CF,func,a);
%copy fit to list
pList(rep,:) = a'; <--- ERROR
ERROR :
Subscripted assignment dimension mismatch.
Error in lse_go (line 59)
pList(rep,:) = a';
I have tried using a.'; , a,' , a' but still it shows the same error. How do I remove this error?

model a periodic time-varing real variable in OpenModelica

What I want to model is a periodic time-varing real variable, the following code can not be simulated. Does somebody have suggestion?
class try
discrete Real x(start = 1);
algorithm
when sample(0,4) then
x := 1; // reinit(x, 1) also does not work
end when;
equation
der(x) = 1;
end try;
All the error message is listed as follows:
Translation 18:32:29 0:0-0:0 Internal error Transformation Module failed!
Translation 18:32:29 0:0-0:0 Internal error BackendDAETransform.reduceIndexDummyDer failed!
Translation 18:32:29 0:0-0:0 Internal error BackendDAETransform.selectDummyState: no state to select
Symbolic 18:32:29 10:3-10:13 Model is structurally singular, error found sorting equations 0.0 = 1.0;
for variables
The problem is that if you want the variable x to be continue between the sampling time istants you have to remove the discrete keyword, this will work fine:
class try
Real x(start = 1);
algorithm
when sample(0,4) then
reinit(x, 1);
end when;
equation
der(x) = 1;
end try;
Ciao,
Marco