Predictor and response variables must have the same length; Matlab - matlab

Thanks in advance for the help.
I am trying to use stepwise regression on a set of data. I have the data in a table, with the single predictor variable on the far right of the table (as a column). Here is what my code looks like.
mdl = stepwiseglm(dummyTrainingTable,'modelspec',modelTech,'Criterion',criterion);
where modelTech and criterion are variables that hold strings dictating two name-value pair options. I am getting the following error
Error using classreg.regr.FitObject/assignData (line 257)
Predictor and response variables must have the same length.
Error in classreg.regr.TermsRegression/assignData (line 349)
model =
assignData#classreg.regr.ParametricRegression(model,X,y,w,asCat,varNames,excl);
Error in GeneralizedLinearModel/assignData (line 794)
model =
assignData#classreg.regr.TermsRegression(model,X,y,w,asCat,dummyCoding,varNames,excl);
Error in GeneralizedLinearModel.fit (line 1165)
model =
assignData(model,X,y,weights,offset,binomN,asCatVar,dummyCoding,model.Formula.VariableNames,exclude);
Error in GeneralizedLinearModel.stepwise (line 1271)
model = GeneralizedLinearModel.fit(X,y,start.Terms,'Distribution',distr,
...
Error in stepwiseglm (line 148)
model = GeneralizedLinearModel.stepwise(X,varargin{:});
This doesn't make sense to me since clearly my response and predictor variables have the same length; they're in a table together. If they weren't the same length, they couldn't be in a table right? Is this an issue with Matlab or is there just something simple that I am missing?
Note, I when I convert the table to a matrix, stepwiseglm runs just fine. i.e.,
dummyTrainingArray = table2array(dummyTrainingTable);
mdl = stepwiseglm(dummyTrainingArray(:,1:size(dummyTrainingArray,2) - 1), dummyTrainingArray(:,size(dummyTrainingArray,2)),modelTech,'VarNames', ...
dummyTrainingTable.Properties.VariableNames,'Criterion', criterion);

I figured out a solution. Although the documentation online states that the input can be a table, when I checked the manual within my version of Matlab (run 'help stepwiseglm'), I found that the function was compatible only with datasets. I then converted my table to a dataset and it ran fine.
Edit, I have Matlab version
8.2.0.701 (R2013b)

'modelspec' is not a valid argument name for the function. Try:
mdl = stepwiseglm(dummyTrainingTable, modelTech, 'Criterion', criterion);

Related

Error during rm ANOVA (Matlab) with random factor

I am trying to run a repeated measures ANOVA in Matlab with 4 factors including one factor representing my subjects which I want as a random factor.
The code I have is as follows:
[p,table,stats] = anovan(COORDS_SUBJ_II,{group_hand,group_stim,group_time,group_subs},'random',4,'varnames',{'HAND','STIM','TIME','SUBS'});
Here, all variables have the same dimension, which is 1350x1(all types are 'double'). I checked my code with some proposed code on the net and it matches, yet I keep getting the following error...
Error using chi2inv (line 3)
P and V must be of common size or scalars
Error in anovan>varcompest (line 838)
L = msTerm .* dfTerm ./ chi2inv(1-alpha/2,dfTerm);
Error in anovan>getRandomInfo (line 811)
[varest,varci] = varcompest(ems,randomterms,msTerm,dfTerm,alpha);
Error in anovan (line 296)
getRandomInfo(msterm,dfterm,mse,dfe,emsMat,randomterm,...
My dependent variable (COORDS_SUBJ_II) has a couple of NaN's in it, although I ran the code once where I replaced those NaN's with random numbers and it still gives me the same error. I am kind of lost right now and would appreciate any help.
Best
ty
Found it out. A toolbox I downloaded a while ago also had the chi2inv command and this prompted the error =)

MatLab Power Law, Non-Positive Value Error

Hi I'm trying to fit a power model to my data using MatLab's fit function
fo = fit(log2(x(:)),log2(y(:)),'power1');
plot(fo,'g'), hold on
However when I run this I get the error
Error using fit>iFit (line 282)
Cannot fit Power functions to data where X has nonpositive values.
Error in fit (line 108)
[fitobj, goodness, output, convmsg] = iFit( xdatain, ydatain, fittypeobj, ...
Error in CurvedPowerLaw (line 20)
fo = fit(log2(x(:)),log2(y(:)),'power1');
When looking at my data and checking if any are less than 1, nothing is displayed
x(count_1)=M(i,1);
y(count_1)=M(i,2);
count_1= count_1+1;
if(M(i,2)<1)
display(M(i,1))
end;
M is a csv file with two columns. I also re ran the code for
if(M(i,1)<1)
and nothing was displayed. Checking manually and nothing seemed to be below 1 either.
i is just the line in the file that is being parsed. The file looks like
344,17
345,13
346,13
347,16
340,12
M(i,1) will result in returning one of the >300 numbers and M(i,2) will return ~10 value
Any help would be much appreciated!!
Thanks
While all values that were parsed in were >0 when scaling them by log2 that's where the 0 values started appearing. A quick fix was to add 1 to each value when parsing them in.

Storing an array/matrix of ClassificationSVM with fitcsvm() function in Matlab gives me an error, how can I resolve it?

I am trying to perform multi-label classification using the SVM libraries in Matlab. There was one solution available for this using the "older" SVM function called svmtrain() here. Based on that, I created my own function using fitcsvm().
However, when I try to store the SVM trained model, I get the following error:
Error using classreg.learning.internal/DisallowVectorOps/subsasgn (line 28)
You cannot assign to an object of class double using () indexing.
Error in multiClassSVM>(parfor body) (line 16)
SVMModel(i) = SVMModelHolder;
Error in multiClassSVM (line 8)
parfor i=1:9
How can I store multiple ClassificationSVM models in a variable?
If it helps, the code is given:
parfor i=1:9
label = (labels==i);
label = i * label;
disp(size(label));
disp(size(trainSet));
SVMModelHolder = fitcsvm(trainSet, label);
disp(class(SVMModelHolder))
SVMModel(i) = SVMModelHolder;
end;
I would appreciate any help with this, or any suggestions to do the problem in a completely different way, if that is the way to go.
fitcsvm outputs an object which can't be stored in a numerical array and thus you must use a cell array (i.e. curly braces) as follows:
SVMModel{i} = SVMModelHolder

MATLAB: findpeaks function

I'm using MATLAB 2013a and trying to find peak points of my data. When I tried code example given in
Find Peaks with Minimum Separation
I am getting the following error:
Error using uddpvparse (line 122)
Invalid Parameter/Value pairs.
Error in findpeaks>parse_inputs (line 84)
hopts = uddpvparse('dspopts.findpeaks',varargin{:});
Error in findpeaks (line 59)
[X,Ph,Pd,Th,Np,Str,infIdx] = parse_inputs(X,varargin{:});
I tried simple x and y vectors and got the same error. What can be the problem?
I have the same problem as you (R2013a on OSX) with the example by the Mathworks. For some reason it seems we can't use findpeaks with the x-and y-data as input arguments, We need to call the function with the y data and use the [peaks,locations] output to get the peaks/plot them.
It looks like in R2014b they changed some stuff about findpeaks that does not work with older versions...like calling the function with not output argument in R2014b plots the data/peaks without any additional step...but it does not for earlier versions.
Anyhow here is a way to workaround the problem. Call findpeaks with a single input argument (y data that is, you can use property/value pairs as well) and use the indices (locations) to show the peaks:
clc
clear
load sunspot.dat
year = sunspot(:,1);
avSpots = sunspot(:,2);
[peaks, locations] = findpeaks(avSpots)
plot(year,avSpots)
hold on
scatter(year(locations),avSpots(locations),40,'filled')
hold off
Output:
It might be worthwhile to contact The Mathworks about this. Hope that helps!

MATLAB: Undefined function 'libsvmwrite' for input arguments of type 'char'

I'm trying to convert a CSV file to LIBSVM/SVMlight format. I found the following code to do it:
SPECTF = csvread('SPECTF.train'); % read a csv file
labels = SPECTF(:, 1); % labels from the 1st column
features = SPECTF(:, 2:end);
features_sparse = sparse(features); % features must be in a sparse matrix
libsvmwrite('SPECTFlibsvm.train', labels, features_sparse);
I used it on Octave on a specific file, and it worked properly.
However, when trying it on MATLAB, I received the error:
Undefined function 'libsvmwrite' for input arguments of type 'char'.
Neither "labels" nor "features_sparse" are chars... (they are doubles). Where is my error? Thanks!
The first argument for libsvmwrite is of type char ( the filename 'SPECTFlibsvm.train'). I think the problem is on the error message given by Matlab. The error message says that the function doesn't exist for input of type char, but most likely it should be that it doesn't exist at all (the message could maybe make sense if Matlab was designed as an OPP language).
Anyway, you simply don't have the libsvmwrite function in your path, or you somehow messed up the installation of the libsvm interface.
Probably you loaded only the source code, which is written in c. You need to compile it. Go to the matlab directory of libsvm and read the instructions.