Bird
Raised Fist0
MATLABdata~20 mins

Regression analysis in MATLAB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Regression Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of simple linear regression coefficients
What is the output of the following MATLAB code that fits a simple linear regression model and displays the coefficients?
MATLAB
x = (1:5)';
y = [2.2; 2.8; 3.6; 4.5; 5.1];
mdl = fitlm(x,y);
disp(mdl.Coefficients.Estimate);
A[1.5; 0.7]
B[1.39; 0.75]
C[2.0; 0.6]
D[0.5; 1.0]
Attempts:
2 left
💡 Hint
Look at the linear relationship between x and y and how fitlm estimates intercept and slope.
❓ data_output
intermediate
1:30remaining
Number of observations after removing missing data
Given the dataset with missing values, how many observations remain after removing rows with NaNs using MATLAB?
MATLAB
data = [1 2; NaN 3; 4 NaN; 5 6];
data_clean = rmmissing(data);
num_obs = size(data_clean,1);
A2
B4
C3
D1
Attempts:
2 left
💡 Hint
rmmissing removes any row with at least one NaN.
❓ visualization
advanced
1:30remaining
Interpretation of residual plot
You run a linear regression and plot residuals versus fitted values. The plot shows a clear curved pattern. What does this indicate?
AResiduals are normally distributed.
BThere is heteroscedasticity but no model misspecification.
CThe linear model fits well with no issues.
DThe model likely misses a nonlinear relationship; consider adding polynomial terms.
Attempts:
2 left
💡 Hint
Look for patterns in residuals to check model assumptions.
🧠 Conceptual
advanced
1:30remaining
Effect of multicollinearity in regression
What is the main problem caused by multicollinearity among predictors in a multiple linear regression model?
AIt causes the model to have perfect predictions.
BIt reduces the number of predictors automatically.
CIt inflates the variance of coefficient estimates, making them unstable.
DIt improves the interpretability of coefficients.
Attempts:
2 left
💡 Hint
Think about how correlated predictors affect coefficient estimates.
🔧 Debug
expert
2:00remaining
Identify error in MATLAB code for polynomial regression
What error does the following MATLAB code produce when fitting a polynomial regression model?
MATLAB
x = (1:5)';
y = [2; 4; 6; 8; 10];
X = [x x.^2];
mdl = fitlm(X, y);
disp(mdl.Coefficients.Estimate);
ANo error; outputs coefficients including intercept and two predictors.
BError: 'fitlm' requires a table or vector for predictors, not a matrix.
CError: Dimension mismatch between X and y.
DError: Missing intercept term in X matrix.
Attempts:
2 left
💡 Hint
Check how fitlm accepts predictor inputs.

Practice

(1/5)
1.

What is the main purpose of regression analysis in MATLAB?

easy
A. To find relationships between variables
B. To sort data alphabetically
C. To create pie charts
D. To calculate matrix inverses

Solution

  1. Step 1: Understand regression analysis goal

    Regression analysis is used to find how one variable depends on others.
  2. Step 2: Match purpose with options

    Only To find relationships between variables describes finding relationships between variables, which is the goal of regression.
  3. Final Answer:

    To find relationships between variables -> Option A
  4. Quick Check:

    Regression = find relationships [OK]
Hint: Regression = relationship finding between numbers [OK]
Common Mistakes:
  • Confusing regression with sorting or plotting
  • Thinking regression calculates matrix inverses
2.

Which MATLAB command correctly fits a linear regression model to data X and response Y?

?
easy
A. mdl = fitlm(X, Y);
B. mdl = linearfit(X, Y);
C. mdl = regress(X, Y);
D. mdl = fitmodel(X, Y);

Solution

  1. Step 1: Recall MATLAB function for linear regression

    The function fitlm fits linear regression models in MATLAB.
  2. Step 2: Check options for correct syntax

    Only mdl = fitlm(X, Y); uses fitlm(X, Y) correctly; others are invalid or incorrect functions.
  3. Final Answer:

    mdl = fitlm(X, Y); -> Option A
  4. Quick Check:

    fitlm = linear model fit [OK]
Hint: Use fitlm for linear regression in MATLAB [OK]
Common Mistakes:
  • Using non-existent functions like linearfit or fitmodel
  • Confusing regress syntax with fitlm
3.

What is the output of this MATLAB code?

X = (1:5)';
Y = [2; 4; 6; 8; 10];
mdl = fitlm(X, Y);
disp(mdl.Coefficients.Estimate(2));
medium
A. 10
B. 2
C. 1
D. 4

Solution

  1. Step 1: Understand data and model

    X is 1 to 5, Y is 2,4,6,8,10 which is Y = 2*X exactly.
  2. Step 2: Interpret coefficient output

    The slope (coefficient for X) is 2, so mdl.Coefficients.Estimate(2) returns 2.
  3. Final Answer:

    2 -> Option B
  4. Quick Check:

    Slope = 2 [OK]
Hint: Slope equals change in Y over X [OK]
Common Mistakes:
  • Confusing intercept with slope coefficient
  • Misreading coefficient index
4.

Identify the error in this MATLAB code for regression:

X = [1 2 3 4 5];
Y = [2; 4; 6; 8; 10];
mdl = fitlm(X, Y);
medium
A. fitlm requires three input arguments
B. fitlm cannot handle numeric data
C. Y must be a matrix, not a vector
D. X and Y should be column vectors, not row vectors

Solution

  1. Step 1: Check input shapes for fitlm

    fitlm expects predictors and response as column vectors or tables.
  2. Step 2: Identify vector orientation

    X is a row vector and Y is a column vector; they should be column vectors for fitlm to work properly.
  3. Final Answer:

    X and Y should be column vectors, not row vectors -> Option D
  4. Quick Check:

    fitlm needs column vectors [OK]
Hint: Use column vectors for fitlm inputs [OK]
Common Mistakes:
  • Passing row vectors instead of column vectors
  • Thinking fitlm needs 3 inputs
  • Believing fitlm can't handle numeric vectors
5.

You have data vectors X and Y with some outliers. Which approach in MATLAB regression helps reduce outlier impact?

hard
A. Remove all data points manually
B. Use fitlm(X, Y) without options
C. Use fitlm(X, Y, ''RobustOpts'', ''on'')
D. Use polyfit(X, Y, 2) for quadratic fit

Solution

  1. Step 1: Understand outlier effect on regression

    Outliers can distort ordinary least squares regression results.
  2. Step 2: Identify MATLAB option for robustness

    Using RobustOpts set to 'on' in fitlm applies robust regression reducing outlier influence.
  3. Final Answer:

    Use fitlm(X, Y, ''RobustOpts'', ''on'') -> Option C
  4. Quick Check:

    RobustOpts = reduce outlier impact [OK]
Hint: Add RobustOpts 'on' to fitlm for outliers [OK]
Common Mistakes:
  • Ignoring outliers and using default fitlm
  • Removing data points without analysis
  • Using polynomial fit which may not reduce outliers
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard