# How to Perform Multivariate Regression with Two Independent Variables on a Surface

3 views (last 30 days)
Hunter on 12 Oct 2022
Edited: Hunter on 12 Oct 2022
Hello, I am in need of some assistance performing multivariate linear regression. I am running into trouble specifically due to my output array being of size (41,2947).
I have tried using mvregress and fitlm but these don't seem to work since my output data I am trying to model an equation for is size (41,2947). Perhaps I am setting things up incorrectly?
Basically, I have two predictor (independent variables) and one output variable (dependent variable) I would like to fit an equation to as shown below.
I was able to get it working when my output array is of size(1,X) or size (X,1) but since my output array is multidimensional and relies on predictor a and b it is giving me trouble.
Any help pointing me in the right direction would be appreciated. Thank you.
%%%%%% Begin MATLAB code%%%%%%%
% First Independent Variable
predictor_a = 1:41;
% Second Independent Variable
predictor_b = 1:2947;
%The Output "surface" I would like to model with an equation
output_variable(1:41,1:2947) = 1000*randn(size(predictor_a,2),size(predictor_b,2));
%Design a meshgrid to plot the 3D surface plot
[aa,bb] = meshgrid(predictor_a,predictor_b);
waterfall(aa',bb',output_variable)
xlabel('Predictor A'); ylabel('Predictor B')
zlabel('Output (Units)')
%%%%%% End MATLAB code%%%%%%%

KSSV on 12 Oct 2022
Hunter on 12 Oct 2022
Edited: Hunter on 12 Oct 2022
Hello KSSV thank you for your response.
This is definitely in the right direction of what I am trying to achieve however there are two concerns I have with this model that are still giving me trouble
1) What if my predictor values are as follows:
predicator_a = [1,2,3, 13,14,15, 25,26,27..etc] (get 3 numbers and skip 10... continue this pattern until size=41)
predicator_b = [1, 2,3,4,5, 30,31,32,33,34,35] (get 5 numbers and skip 25.... continue this pattern until size=2947)
First off, the problem with this is the two predictors are not the same size and they are not linearly increasing (i.e. they are not 1,2,3,4,5,6,7,8,9..41 or 1,2,3,4,5,6,7,8,9...2497).
I can't make the predictor arrays square (same size) since they are non linearly increasing either it seems like and there is a threshold that the data caps out at and changes behavior.
(If i added more points past 41 to get the array size of predictor_a up to 2947 the behavior of the system would change in the real model. The real model also contains only discrete values as control variables (independent variables), no non-integer values)
Is there a way to predict with two matrices of different sizes? (i.e size_A = (1,41) size_B=(1,2947)?
2) the fit method seems to only account for one row or column at a time in my output data. i.e. (1,:) or (:,1) - is that right? Because when i enter output variable (z in fit) as (1:41,1:2947) it is saying it can only operate on one column vector at a time, i.e the size of z must be 2947,1. Is it really operating on all the 2D matrix data when it finds the equation or only one column?
Would I have to do something like what's shown below to get an equation for each column in the entire surface rather than only 1 column at a time?
for i =1:2947
eval(sprintf(["surffit%d = fit([predictor_a(:,1),predictor_b(:,1)],output_variable(:,i),'poly23','normalize','on');"],i));
end
In the example file from fit documentation (load franke) the array sizes are all (293x1 for x,y,z) this seems to work since they are all the same size. Is there something that works when arrays are different sizes (i.e an output array size (41,2947) and two input predictor arrays size a=(1,41) size b= (1,2947)?
The following code works as a thought experiment but what if the arrays could not be square? I.e. i could not convert predictor_a up to 2947 samples? And why does it only include output variable (:,1) in the Z parameter? What about the other 2946 columns?
predictor_a = linspace(1,41,2947)';
predictor_b = (1:2947)';
output_variable(1:size(predictor_a,1),1:size(predictor_b,1)) = 1000*randn(size(predictor_a,2),size(predictor_b,2));
surffit = fit([predictor_a(:,1),predictor_b(:,1)],output_variable(:,1),'poly23','normalize','on')

### Categories

Find more on Multiple Linear Regression in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!