简体   繁体   English

MatLab中的简单逻辑回归 - 需要初学者帮助

[英]Simple logistic regression in MatLab - beginner help required

I'm trying to do a simple logistic regression analysis in MatLab. 我正在尝试在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)

I keep getting this error: 我一直收到这个错误:

If Y is a column vector, it must contain positive integer category numbers.

I'm not sure why. 我不知道为什么。 Can someone please help?! 有人可以帮忙吗?! Thanks! 谢谢!

Pls read the documentation of mnrfit: 请阅读mnrfit的文档:

https://www.mathworks.com/help/stats/mnrfit.html#btmaowv-Y https://www.mathworks.com/help/stats/mnrfit.html#btmaowv-Y

Try use table and then let Y be the categorical array. 尝试使用表,然后让Y成为分类数组。

For example, my code: 例如,我的代码:

%% 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'); 

hope this helps. 希望这可以帮助。

Logistic regression is used when dependent variable namely variable y is a binary number 0 or 1. Nominal Logistic Regression is quite wide as dependent variable could take more than 2 values, but they have to be consecutive natural numbers. 当因变量即变量y是二进制数0或1时,使用逻辑回归。标称Logistic回归非常宽,因为因变量可能需要2个以上的值,但它们必须是连续的自然数。 For example Y = 0, 1, 2, 3, ... X, the independent variable doesn't have this restriction it can be any reel number. 例如,Y = 0,1,2,3,... X,自变量没有这个限制,它可以是任何卷轴数。

To use mnrfit proceed as follow 要使用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 

There are 3 categories : O small, 1 medium, 2 large Following the logic above 有3个类别:O小,1中,2大遵循上面的逻辑

Y = [2 2 0 1 1 1] Y = [2 2 0 1 1 1]

Type the following code in matalb and check 在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);

According to your Y data format, I suggest you use polynomial linear regression model instead of logistic regression since your Y values are not discrete. 根据您的Y数据格式,我建议您使用多项式线性回归模型而不是逻辑回归,因为您的Y值不是离散的。

Polynomial linear regression 多项式线性回归

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM