-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulate_data1.m
More file actions
48 lines (41 loc) · 1.28 KB
/
Copy pathsimulate_data1.m
File metadata and controls
48 lines (41 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
%% Data generation
% hajjem
function [X, s, y, F] = simulate_data1(n, m, sa, sb)
% Initial parameter
rho=0.1;
sigma1=1;
sigma=1;
% Generate random predictor variables
X = normrnd(0, 1, n, 9);
% Generate spatial locations
s = sa+(sb-sa)*rand(m, 2);
% Generate covariance matrix for the random effects
Sigma = zeros(m, m);
for i = 1:m
for j = 1:m
distance = norm(s(i, :) - s(j, :)); % Compute the Euclidean distance
r = exp(-distance/rho);
Sigma(i, j) = sigma1^2 * r;
end
end
% Generate random effects
b = mvnrnd(zeros(m, 1), Sigma)'; % covariance matrix Sigma
Z = eye(n, m);
b1 = Z * b;
% Generate fixed effects
F = zeros(n, 1);
for i = 1:length(X)
x1 = X(i,1);
x2 = X(i,2);
x3 = X(i,3);
%F(i) = 2 * X(i) + X(i)^2 + 4 * (X(i) > 0) + 2 * log(abs(X(i))) * X(i);
F(i) = 2 * x1 + x2^2 + 4 * (x3 > 0) + 2 * log(abs(x1)) * x3;
%F(i) = 2 * x1 + x1^2 + 4 * (x1 > 0) + 2 * log(abs(x1)) * x1;
end
C = sqrt(var(F));
F = F / C;
% Generate error term
epsilon = normrnd(0, sigma, n, 1);
% Generate response variable
y = F + b1 + epsilon;
end