Please help me with my code ( linear regression)

if true
% code
endclear; clc;
%Step1:Generate a vector Y and X
n=1000;
v=2;
Y=trnd(v,n,1);
x1=ones(n,1);
x2=randn(n,1);
X=[x1 x2];
%Step2:Compute the OLS estimator
beta=(X'*X)^(-1)*X'*Y;
%Step3: Repeat 1. and 2. MC = 10000 times.
MC= 10000;
for nsample = 1:5
if nsample == 1; n = 1000; end;
if nsample == 2; n = 2000; end;
if nsample == 3; n = 3000; end;
if nsample == 4; n = 5000; end;
if nsample == 5; n = 7000; end;
for n=1:MC;
beta_hat(n,:) = (X'*X)^(-1)*X'*Y
for df=1:10;
B0=beta_hat(n,:)- beta;
bias=(1/MC)*symsum(B0,n,1,MC);
rmse=sqrt(bias^2);
df=[1:0; 1.1; 1.2; 1.3; 1.4; 1.6; 1.8; 2.0; 5.0; 10.0];
end;
end;
end;
The statements I am trying to code is below

5 Kommentare

the cyclist
the cyclist am 30 Mär. 2017
What help do you need? What is your question? Are you getting an unexpected result? In what way?
Are you getting an error message? If so, what is it?
Mark Lu
Mark Lu am 31 Mär. 2017
Bearbeitet: Mark Lu am 31 Mär. 2017
Hi the cyclist, Thanks for answering. Well, when I run the code it is not working. I wonder where I did wrong. Without computing the biais and the rmse it works. Please help me
the cyclist
the cyclist am 31 Mär. 2017
It doesn't help to say "it is not working". What do you mean, specifically? Does the program crash, and give an error? What is the full error message? Does it give an incorrect result? How do you know it is incorrect?
Please ... help us help you!
Mark Lu
Mark Lu am 31 Mär. 2017
Here is the full error message . ''Error using - Matrix dimensions must agree. Error in Assign3 (line 27) B0=beta_hat(n,:)- beta;''
Image Analyst
Image Analyst am 3 Apr. 2017
What are the sizes of beta_hat and beta? How many rows and columns? Evidently they're not the same! beta_hat(n, :) is a 1-D row vector and you're probably trying to stuff a 2-D matrix into a single row and it won't fit.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Nicolas Schmit
Nicolas Schmit am 5 Sep. 2017

0 Stimmen

The error message means that you are trying to subtract two variables with incompatible sizes. This occurs because size(beta)= [2 1] whereas size(beta_hat(n,:)) = [1 2]. Transpose beta to make it a row vector.

Kategorien

Gefragt:

am 30 Mär. 2017

Beantwortet:

am 5 Sep. 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by