generate random sample for linear regression

13 ビュー (過去 30 日間)
Abas Arkawazi
Abas Arkawazi 2020 年 7 月 16 日
コメント済み: Abas Arkawazi 2020 年 7 月 20 日
Hi guys
can anyone help me how to generate random sample for y, x1 and x2 in the following model (multiple linear regression):
yi= a + bx1 + cx2 + e
where a, b and c are parameters and e is random error.

採用された回答

Jeff Miller
Jeff Miller 2020 年 7 月 17 日
% First you have to define the characteristics of x1, x2, and e.
% These can be any values you want, such as:
mu1 = 0; % Mean of x1
sd1 = 1; % Standard deviation of x1
mu2 = 100;
sd2 = 10;
rhoxy = 0.3; % the true correlation of x1 and x2
mue = 0; % Mean of e, usually assumed to be zero
sde = 5; % Standard deviation of e
a = 4; % Known parameters of your regression model
b = 3;
c = 2;
nPoints = 100; % the number of data rows you want to generate
% The rest just uses those values:
mu = [mu1, mu2];
sigma = [sd1^2 sd1*sd2*rhoxy; sd1*sd2*rhoxy sd2^2];
x = mvnrnd(mu,sigma,nPoints);
e = mue + sde*randn(nPoints,1);
y = a + b*x(:,1) + c*x(:,2) + e;
  1 件のコメント
Abas Arkawazi
Abas Arkawazi 2020 年 7 月 20 日
Thank u very much

サインインしてコメントする。

その他の回答 (1 件)

Abhishek Gangwar
Abhishek Gangwar 2020 年 7 月 16 日
You can generate random values for x1, x2 using "randperm()" function and then find out corresponding 'y' values,
x1 = randperm(100);
x2 = randperm(100);
and put these values into your equation to find out corresponding 'y' values.
Note: 'randperm()' function gives you the random permutation of number from 1....n.
other functions, for example 'rand()', 'randn()' generates decimal numbers.
  1 件のコメント
Abas Arkawazi
Abas Arkawazi 2020 年 7 月 20 日
Thank u very much

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLinear and Nonlinear Regression についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by