MATLAB Answers

Error in code.

4 views (last 30 days)
keerthi appala
keerthi appala on 5 Nov 2020
Answered: Sean de Wolski on 5 Nov 2020
Hi,
I am trying to run the following code it is showing me the following error.Can someone explain me what's wrong.
Thanks
%Answer 2
%with 5 variables
mdl1 = stepwiselm(beer_spectra,extract_data,'Upper','linear','Criterion','bic','NSteps',5);
1. Adding *464, BIC = 49.834
2. Adding *368, BIC = -17.5899
3. Adding *559, BIC = -24.1202
4. Adding *644, BIC = -28.8595
5. Adding *380, BIC = -35.021
mdl1 =
Linear regression model:
y ~ 1 + x368 + x380 + x464 + x559 + x644
Estimated Coefficients:
Estimate SE tStat pValue
________ ______ _______ __________
(Intercept) 148.96 10.548 14.122 8.7499e-16
x368 58.286 14.747 3.9524 0.00037113
x380 40.477 13.137 3.0812 0.0040682
x464 -123.12 4.7784 -25.766 6.8252e-24
x559 11.471 2.4232 4.7336 3.7951e-05
x644 8.6249 2.0201 4.2695 0.00014862
Number of observations: 40, Error degrees of freedom: 34
Root Mean Squared Error: 0.128
R-squared: 0.998, Adjusted R-Squared: 0.997
F-statistic vs. constant model: 2.8e+03, p-value = 1.96e-43
%with 10 variables
mdl2 = stepwiselm(beer_spectra,extract_data,'Upper','linear','Criterion','bic','NSteps',10)
1. Adding x464, BIC = 49.834
2. Adding x368, BIC = -17.5899
3. Adding x559, BIC = -24.1202
4. Adding x644, BIC = -28.8595
5. Adding x380, BIC = -35.021
6. Adding x848, BIC = -40.5982
7. Adding x648, BIC = -46.2822
8. Adding x731, BIC = -50.83
9. Adding x549, BIC = -57.7006
10. Adding x868, BIC = -65.974
mdl2 =
Linear regression model:
y ~ 1 + x368 + x380 + x464 + x549 + x559 + x644 + x648 + x731 + x848 + x868
Estimated Coefficients:
Estimate SE tStat pValue
________ _______ _______ __________
(Intercept) 152.26 8.7001 17.501 5.9138e-17
x368 41.635 9.6744 4.3036 0.00017445
x380 60.569 8.4066 7.2049 6.2305e-08
x464 -131.09 3.0401 -43.121 7.4363e-28
x549 4.5534 1.4022 3.2473 0.0029397
x559 13.369 1.4883 8.9826 7.1006e-10
x644 11.588 1.2481 9.2848 3.4568e-10
x648 -6.4651 1.4485 -4.4632 0.00011239
x731 -2.1068 0.45986 -4.5814 8.1059e-05
x848 -3.7921 0.80907 -4.6871 6.0491e-05
x868 2.7459 0.86363 3.1795 0.0034969
Number of observations: 40, Error degrees of freedom: 29
Root Mean Squared Error: 0.075 %RMSE is lower than previous model
R-squared: 0.999, Adjusted R-Squared: 0.999 %R-squared value is closer to 1 than previous model
F-statistic vs. constant model: 4.12e+03, p-value = 6.69e-43
%I choose this model for further analysis.
%Plot
figure;
scatter(extract_data,mdl2.Fitted);
xlabel("extract value (measured)");
ylabel("extract value (predicted)");
str = {'Rsquared = 0.999'};
text(-8,6,str)
idx = training(c,1);
regf=@(beer_spectra,exctract_data,beer_test_spectra)([ones(size(beer_test_spectra,1),1) beer_test_spectra]*regress(extract_data,[ones(size(beer_spectra,1),1) beer_spectra]));
%Using lasso()
[B, FitInfo] = lasso(beer_spectra,extract_data,'CV',10);
FitInfo =
struct with fields:
Intercept: [1×51 double] %#ok<BADCH>
Lambda: [1×51 double] %#ok<BADCH>
Alpha: 1
DF: [33 31 28 28 27 25 23 22 22 20 17 18 16 15 15 15 12 11 7 3 3 3 3 3 3 3 3 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0]
MSE: [1×51 double] %#ok<BADCH>
PredictorNames: {}
SE: [1×51 double] %#ok<*BADCH>
LambdaMinMSE: 0.0226
Lambda1SE: 0.0914
IndexMinMSE: 1
Index1SE: 16
lassoPlot(B,FitInfo,'PlotType','CV');
legend('show')
idxLambda1SE = FitInfo.Index1SE;
b = B(2:end,idxLambda1SE);
b0 = FitInfo.Intercept(idxLambda1SE);
plot(b);
xlabel('variable')
ylabel('regression coefficient')
title('Plot of regression coefficients')
%plot of measured vs predicted extract value
yhat = beer_test_spectra*b + b0;
scatter(extract_test_data,yhat)
hold on
plot(extract_test_data,extract_test_data)
xlabel('measured extract value') ;
ylabel('predicted extract value');
title('Plot of measured and predicted extract value')
%RMSECV and RMSEP
idx = training(c,1);
regf=@(beer_spectra,extract_data,beer_test_spectra)([ones(size(beer_test_spectra,1),1) beer_test_spectra]*regress(extract_data,[ones(size(beer_spectra,1),1) beer_spectra]));
RMSECV1 = sqrt(crossval('mse',beer_spectra,extract_data,'predfun',regf,'kfold',10))
RMSECV1 =
3.7016
RMSEP1=sqrt(sum((extract_test_data-yhat).^2)/size(beer_test_spectra,1))
RMSEP1 =
0.1398
%Cross-validation
C_PCR = cvpartition(926,'Kfold',10);
for i = 1:10
trIdx = C_PCR.training(i);
teIdx = C_PCR.test(i);
yhat_cv = polyval(polyfit(trIdx,extract_data,2),beer_test_spectra);
sse = sse + sum((yhat_cv - extract_test_data).^2);
end
%plot
figure;
plot(Y,Yhat_cv,'o',extract_test_data,yfit,'r-');
hold on
axis square
xlabel('Observed y Values')
ylabel('Predicted y Values')
title('Plot of measured vs predicted extract value')

  2 Comments

Sean de Wolski
Sean de Wolski on 5 Nov 2020
What error are you getting?
keerthi appala
keerthi appala on 5 Nov 2020
Error: File: Answer2.m Line: 4 Column: 4
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax
error. To construct matrices, use brackets instead of parentheses.
This is the error i am getting.

Sign in to comment.

Answers (1)

Sean de Wolski
Sean de Wolski on 5 Nov 2020
It looks like you just copied someone's script that has the outputs as well as the code. You can't run the outputs.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by