Hi, i've been trying to convert some code from matlab into R, but am stumbling with mirroring the 'sparse' function from matlab into r. I've used sparseMatrix in r, but what i'm entering doesnt provide the same output. Below is the matlab code:
x_part = sparse(x == x_unique (1:end-1)');
y_part = sparse(y == y_unique (1:end-1)');
X = [sparse(true(n,1)),x_part,y_part];
Y = sparse(score(:));
coeffs = (X'*X)\(X'*Y);
Yhat = full(X*coeffs);
In r, I've tried:
x_part <- Matrix(t(x == x_unique[1:length(x_unique)-1]), sparse = T)
y_part <- Matrix(t(y == y_unique[1:length(y_unique)-1]), sparse = T)
X <- Matrix(matrix(1,n),x_part, y_part, sparse = T);
Y <- Matrix(score, sparse = T);
coeffs <- (t(X)*X) / (t(X)*Y)
Yhat <- full(X*coeffs)
...but it doesn't work.
Any thoughts on how to solve this within R, would be greatly appreciated.
Thanks in advance.