# How do I implement Linear regression with leave-one-out cross validation in MATLAB?

조회 수: 109(최근 30일)
Jhon Gray 2020년 5월 31일
답변: Satadru Mukherjee 2020년 10월 29일
I have a data set of 87 variables and 1 outcome where all are continuous. I need to use linear regression with leave-one-out cross validation to create a model/equation with prediction's accuracy, sensitivity, and specificity.
• Can I use Regression Learner app for this? If yes how to get quation from a created model in Regression Learner app?
• Can/Should I use cross validation to divide my data set and get the results?
• Is there any code sample on how to go about doing this? (I'm new to all of this)
• Should I be using stepwiselm or fitlm or glmfit/glmval? What's the difference and how do I choose?

댓글을 달려면 로그인하십시오.

### 채택된 답변

Amogh Bhole 2020년 6월 19일
Hi,
In order to use Linear regression with cross validation you need to use fitrlinear, refer to this link for more details.
To apply leave one out cross validation use kfold keeping the value of k as the total number of observations in the data. Refer to this link.
You will find the answer to rest of the questions in the above links.

댓글을 달려면 로그인하십시오.

### 추가 답변(1개)

Satadru Mukherjee 2020년 10월 29일
Demo code of Implementation linear regression with leave-one-out cross validation in MATLAB
Note: I have tried to avoid the inbuilt functions to create the model or to cross validate or to calculate coefficient of determination , so that we can get the complete feeling out of the code:-)
clc
clear all
close all
warning off
x=table2array(data(:,1))';
y=table2array(data(:,2))';
n=length(x);
predictions=[];
for p=1:n
trainindex=setdiff(1:n,p);
testindex=p;
xtrain=x(trainindex);
ytrain=y(trainindex);
xtest=x(testindex);
a=[];
for i=1:length(xtrain)
a=[a ; xtrain(i) 1];
end
c =a\ytrain';
ytest = c(1)*xtest + c(2);
predictions=[predictions ytest];
end
r=y-predictions;
r=r.^2;
rsq=(1-(sum(r)/sum((y-mean(y)).^2)))*100;
disp(rsq);

댓글을 달려면 로그인하십시오.

### 범주

Find more on Support Vector Machine Regression in Help Center and File Exchange

R2019b

### Community Treasure Hunt

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

Start Hunting!

Translated by