I want to use ARX. X is a 1000X13 matrix (1000 sample with 13 features). I want to see the relationship of for example 1st and 2nd column of X. I don't know how to make input parameters right. What should be the size of [na nb nk] for my regression problem. Matlab documentation doesn't have much detail.
Here is my code:
data = iddata(X(:,1),[],1); %I have to make iddata object first.
Y = arx(data,[ [ones(size(X(:,1),2),size(X(:,1),2))] [ones(size(X(:,2),1),size(X(:,1),1))] [ones(size(X(:,1),2),size(X(:,1),1))] ])
Error is:
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
I tried to change the dimensions of [na nb nk], but every time, I got an error like:
Y = arx(data,[ [ones(size(X(:,1),2),size(X(:,1),2))] 1 [ones(size(X(:,1),2),size(X(:,1),1))] ])
Invalid ARX orders. Note that continuous-time ARX models are not supported.
Y = arx(data,[ 1 1 1])
Error using arx (line 77)
The model orders must be compatible with the input and output dimensions of the estimation data.
Related
Background : I want to implement a MATLAB algorithm that takes as input vectors x and y, solves the linear regression problem
associated with the data stored in x and y using a modified QR version and then to plot the graph of the linear function.
So first I wrote the modified QR algorithm :
function x=QRQ(A,b,n)
[Q1,R1]=qr(A);
c1=Q1'*b;
n=length(c1);
x=backward(R1,c1,n);
end
function x=backward(U,y,n)
x=zeros(n,1);
x(n)=y(n)/U(n,n);
for i=n -1 : -1 : 1
x(i)=(y(i)-U(i,i+1 : n)*x(i+1 : n))/U(i,i);
end
end
Then I wrote the algorithm for the linear regression :
function ysol = LinearReg(x,y)
A=[x ones(21,1)];
z=QRQ(A,y,2);
ysol=z(1)*x+z(2);
plot(x,y,'bo',x,ysol,'g-');
end
I tried to run this algorithm on the following data :
x=[0;0.25;0.5;0.75;1;1.25;1.5;1.75;2;2.25;2.5;2.75;3;3.25;3.5;3.75;4;4.25;4.5;4.75;5];
y=[4;3;7;7;1;4;4;6;7;7;2;6;6;1;1;4;9;3;5;2;7];
The full error message that I received is :
Index in position 2 exceeds array bounds (must not exceed 2).
Error in untitled>backward (line 12)
x(n)=y(n)/U(n,n);
Error in untitled>QRQ (line 8)
x=backward(R1,c1,n);
Error in untitled>LinearReg (line 20)
z=QRQ(A,y,2);
The line causing the error is x(n)=y(n)/U(n,n);
the only variable with an index in position 2 is U. Apparently U only has 2 columns, and n is a value >2, hence the error.
Using the debugger, I see that U is a 21x2 array, and n has a value of 21.
How can this MATLAB algorithm be fixed?
The U in your case is R1. Since the matrix A has rank two, R1 will only have two columns since A already only has two columns.
You then try to solve the system R1 * x = y using backward substitution with the index starting at n, but here you clearly have to start at 2.
(Keep in mind that R1 is an upper triangular matrix.)
My teacher has given me a task of expressing a sound file(.wav) in polynomial form in MATLAB(curve fitting),.
I tried using polyfit() like this:-
a=wavread('filename');
x=linspace(0,1,239915);
p=polyfit(x,a,4);
display(p);
but I am getting error as
X and Y vectors must be the same size
Please help me.
Size of a is not 239915, it is for x we have taken these many samples, for line space, by the way, why do we need x for polyfit().
Thank you in advanced
Try this
a = wavread('filename')
x = linspace(0, 1, numel(a))';
p = polyfit(x,a,4);
display(p)
Note that I transpose linspace output as it returns a row vector where wavread returns a column vector, which is why you get the error message.
The below code gives me a error:
Subscripted assignment dimension mismatch.
Error in ==> lookmcvmt at 18
M(:,:,j,i) = mcmvOUT2((k+1):(k+Nz), i:Nt:Nr);
Please help to solve.
load MCMVout1xzy
mcmvOUT2 = MCMVout1xzy;
whos
[Nr2 Nr] = size(mcmvOUT2);
Ny = 51;
Nx = 51;
Nz = 41;
Nt = 10;
M = zeros(Nz,Nx,Ny,Nt);
for j=1:Ny
for i=1:Nt
k = Nz*(j-1);
M(:,:,j,i) = mcmvOUT2((k+1):(k+Nz), i:Nt:Nr);
end
end
The error 'subscripted assignment dimension mismatch' means you are trying to assign a block of values into a space that is the wrong size.
This entity
mcmvOUT2((k+1):(k+Nz), i:Nt:Nr);
represents a matrix of values in 2 dimensions. Its size is defined by the two ranges specified by (k+1):(k+Nz) and i:Nt:Nr - you can check its size by typing
size(mcmvOUT2((k+1):(k+Nz), i:Nt:Nr))
The space you are trying to fit it into has to be exactly the same dimensions. The size of the range specified by
M(:,:,j,i)
is defined by the Nz and Nx arguments to the zeros call with which you preallocated the array.
We can't test this because the MCMVout1xzy file containing your data is not given, but you will be able to solve this yourself by using the size command and making sure all your dimensions match.
Because matlab uses column-wise indexing, and a lot of us are used to the row-wise paradigm of cartesian coordinate systems, getting the order of your indexes right can be confusing - this is the root of a lot of these kinds of error (for me anyway).
Things to check: your dimensions Nz etc. are correct and the order of your Nz etc. variables in the zeros call is correct.
So I'm using an arima(3,0,0) model to forecast values in Matlab. I have a vector of initial values for it to use to forecast off of but I keep getting an error.
Here is my code:
model1=arima(3,0,0);
[EstMdl1,~,logL1]=estimate(model1,qtrdatachangelog2000(:,1));
Ymdl1pred=zeros(length(qtrdatachangelogafter2000),1);
for i=1:length(qtrdatachangelogafter2000)-2;
[Ymdl1pred(i)]=forecast(EstMdl1,1,'Y0',qtrdatachangelogafter2000(i+2,1));
end;
The error I get:
Error using internal.econ.LagIndexableTimeSeries.checkPresampleData (line 653)
Number of rows in presample array 'Y0' must be at least 3.
Error in arima/forecast (line 498)
Y0 = internal.econ.LagIndexableTimeSeries.checkPresampleData(zeros(maxPQ,numPaths),
'Y0', Y0, OBJ.P);
I'm assuming this is because the AR(3) has 3 parameters and thus needs at least 3 rows of data before it can start which is why in my for loop I used i+2 but it continues to give the error. Please help.
Using i+2 you are not adding 3 rows, you are using only 1 row. You need to specify the start and end index: i:i+2. You can use this code:
[Ymdl1pred(i)]=forecast(EstMdl1,1,'Y0',qtrdatachangelogafter2000(i:i+2,1));
I am trying to model a vector data containing 200 sample points denoting a measurement.I want to see "goodness of fit" and after reading I found that this can be done by predicting the next set of values(I am not that confident though if this is the correct way).I am stuck at this since the following code gives an error and I am just unable to solve it.Can somebody please help in removing the error
Error using *
Inner matrix dimensions must agree.
Error in data_predict (line 27)
ypred(j) = ar_coeff' * y{i}(j-1:-1:j-p);
Also,can somebody tell me how to do the same thing i.e get the coefficients using nonlinear AR modelling,moving average and ARMA since using the command nlarx() did not return any model coefficients?
CODE
if ~iscell(y); y = {y}; end
model = ar(y, 2, 'yw');
%prediction
yresiduals=[];
nsegments=length(y);
ar_coeffs = model.a;
ar_coeff=[ar_coeffs(2) ar_coeffs(3)]
for i=1:nsegments
pred = zeros(length(y{i}),1);
for j=p+1:length(y{i})
ypred(j) = ar_coeff(:)' * y{i}(j-1:-1:j-p);
end
yresiduals = [yresiduals; y{i}(p+1:end) - ypred(p+1:end)];
end
In matlab, * is the matrix product between two matrices. That means that the number of columns in the first matrix must equal the number of rows in the second matrix. You might have intended to use .* element by element multiplication. EDIT: For element by element multiplication, the matrices must be the same size. Check the size of your matrices. If they don't fit either of these conditions, something needs to change.