[英]Simple logistic regression in MatLab - beginner help required
我正在嘗試在MatLab中進行簡單的邏輯回歸分析。
X = [103.4843 103.4843 100.3871 101.8535 101.7658 101.9658];
Y = [120.9189 107.3617 122.5506 96.9701 101.9798 118.3035];
B = mnrfit(X,Y)
我一直收到這個錯誤:
If Y is a column vector, it must contain positive integer category numbers.
我不知道為什么。 有人可以幫忙嗎?! 謝謝!
請閱讀mnrfit的文檔:
https://www.mathworks.com/help/stats/mnrfit.html#btmaowv-Y
嘗試使用表,然后讓Y成為分類數組。
例如,我的代碼:
%% Multinomial Logistic Regression
% read csv file and create table
% header = {'Year','Abortion','DowJones','Incarceration','Crime_Rate'};
data = csvread("E:\code\project\regression.csv",1,0);
year = data(:,1);
abortion = data(:,2);
dowjones = data(:,3);
incarceration = data(:,4);
crime_rate = data(:,5);
T = table(year,abortion,dowjones,incarceration,crime_rate);
% multinomial logistic regression
X = [abortion,dowjones,incarceration];
Y = categorical(crime_rate);
% B: coefficicent estimates
% dev: deviance of the fit
% stats: model statistics
[B,dev,stats] = mnrfit(X,Y,'Model','ordinal','link','logit');
希望這可以幫助。
當因變量即變量y是二進制數0或1時,使用邏輯回歸。標稱Logistic回歸非常寬,因為因變量可能需要2個以上的值,但它們必須是連續的自然數。 例如,Y = 0,1,2,3,... X,自變量沒有這個限制,它可以是任何卷軸數。
要使用mnrfit,請按以下步驟操作
X = [103.4843 103.4843 100.3871 101.8535 101.7658 101.9658];
if X > 103 --> X large --> translated to Y = 2
if 101 < X < 103 --> X medium --> translated to Y = 1
if X < 101 --> X small--> translated to Y = 0
有3個類別:O小,1中,2大遵循上面的邏輯
Y = [2 2 0 1 1 1]
在matalb中鍵入以下代碼並檢查
X = [103.4843 103.4843 100.3871 101.8535 101.7658 101.9658];
Y = [2 2 0 1 1 1];
Y = categorical(Y);
B = mnrfit(X,Y);
根據您的Y數據格式,我建議您使用多項式線性回歸模型而不是邏輯回歸,因為您的Y值不是離散的。
多項式線性回歸
X = [103.4843 103.4843 100.3871 101.8535 101.7658 101.9658];
Y = [120.9189 107.3617 122.5506 96.9701 101.9798 118.3035];
B = polyfit(X,Y,length(X)-1);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.