[英]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.