I am trying to implement the process below with my own data. The data is in a spreadsheet with headers, and the first column has dates, the other columns have returns.
https://inovancetech.com/hmm-tutorial-1.html
My variation on the code:
Returns <- read.csv("MyData.csv",header=TRUE,stringsAsFactor=F)
Date<-as.character(Returns[,1])
DateTS<- as.POSIXlt(Date, format = "%m/%d/%Y")
TSData<-data.frame(Returns[,2:6],row.names=DateTS)
TSData<-as.xts(TSData)
MyReturns <- TSData[-1,1]
ModelData<-data.frame(MyReturns)
set.seed(1)
HMM<-depmix(MyReturns~1,data=ModelData,nstates=2,family=gaussian())
This is where I get an error:
Error in dimnames(cd) <- list(as.character(index(x)), colnames(x)) :
'dimnames' applied to non-array
I can't figure out why. Any hints?
When I ran the code I got no error. However, try this:
HMM<-depmix(list(MyReturns~1),data=ModelData,nstates=2,family=gaussian())
I got a similar error elsewhere because I was sharing the data frame variable name indirectly instead of the data itself.
Related
Here is the shape of my array
b = data[0].values
print(b.shape)
(5126, 4229)
I get this error when I run this code:
from impyute.imputation.cs import mice
# start the MICE training
a=mice(b)
Error:
ValueError: Expected 2D array, got 1D array instead:
array=[].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
I am confused by this error message, any recommendations?
First, you must change your input data to a 2D array, so you must specify the number of features in your data using reshape function.
Please try to use b.reshape(5126, 4229), if not try to follow this example until you figure out the problem
Does anyone know how to read this file (array of array, i think??) with retrofit. Can't find any examples on the internet. Have tried
1. ArrayList
2. Collection
3. []
4. [][]
5. List
Error:
Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 3 path $[0]
File:
[[1518173474652,"0.10002000","0.10010300","0.10000100","0.10009300","51.46200000",1518173534651,"5.14685961",86,"27.11500000","2.71207304","0"],[1518173534652,"0.10009300","0.10025000","0.10009300","0.10010800","122.42800000",1518173594651,"12.25782098",123,"20.10800000","2.01471571","0"]]
I think you're parsing it as a JSON, because of the error you received :
Error: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 3 path $[0]
That file, unfortunately, is not a JSON file, it doesn't fulfill the JSON syntax requirement. To check this, copy-paste your file into any JSON formatter online and they can validate it for you.
You could try parsing it with a custom-made converter. Example here.
I have a data frame DF which contains numerous variables. Each variable is present twice because I am conducting an analysis of "couples".
Among others, DF has a series of indicators of diversity :
DF$div1.1, DF$div2.1, .... , DF$divN.1, DF$div.1.2, ..., DF$divN.2
Similarly, it has a series of indicators of another characteristic:
DF$char1.1, DF$char2.1, .... , DF$charM.1, DF$char.1.2, ..., DF$charM.2
Here's a link to an example of DF: http://shorttext.com/5d90dd64
Each time the ".1", ".2" stand for the couple member considered.
My goal:
For each indicator divI and charJ, I want to create another variable DF$divchar that takes the value DF$divI.1 when DF$charJ.1>DF$charJ.2; and DF$divI.2 when DF$charJ.1<DF$charJ.2.
Here is the solution I came up with, it seems somehow very intricate and sometimes behaves in strange ways:
I created a series of binary variables that take the value one if DF$charJ.1>DF$charJ.2. The are stored under DF$CharMax.1.
Here's how I created it:
DF$CharMax.1 <- as.data.frame(
sapply(1:length(nam),
function(n)
as.numeric(DF[names(DF)==names.1[n]]
>DF[names(DF)==names.2[n]])
))
I created the function BinaryExtract:
BinaryExtract <- function(var1, var2, extract) {var1*extract +var2*(1-extract)}
I created the matrix NameFull that contains all the possible combinations of div and char, separated with "YY"
NameFull <- sapply(c("div1",...,"divN")
, function(nam) paste(nam, names(DF$YMax.1), sep="YY")
And then I create all my variables:
DF[, as.vector(NameFull)] <- lapply(as.vector(NameFull), function(e)
BinaryExtract(DF[,paste0(unlist(strsplit(e,"YY"))[1],".1")]
, DF[, paste0(unlist(strsplit(e,"YY"))[1],".1")]
, DF$charMax.1[unlist(strsplit(e,"YY"))[2]]))
My Problem
A. It looks like a very complicated solution for something that simple. What am I missing?
B. Moreover, when I print DF, just typing DF in the command window, I do not see the variables NameFull. They seem to appear with the names of char.
Here's what I get: http://shorttext.com/5d9102c
Similarly, I have tried to change all their names to get rid of the "YY" and it does not seem to work:
names(DF[, as.vector(NameFull)]) <- as.vector(c("div1",...,"divN"), sapply(, function(nam)
paste(nam, names(DF$YMax.1), sep=".")))
When I look at names(DF), I keep getting the old names with the "YY"
However, I do get a result if I explicitly call for them
> DF[,"divIYYcharJ"]
I would really appreciate any suggestion, comment and explanation. I am quite new to R ad was more used to Stata. I feel there is something deeply inefficient here. Thanks
I have an object in Matlab created from a third party toolbox. Within the object is a 3x65 double array. If I type the name of the object in the Matlab console, it lists all the contents, and specifically says this 3x65 array is a double. All I want to do is to extract this array into a separate Matlab array. But when I do something like:
x = object.ArrayIWant
I get the error "Access to an object's fields is only permitted within its methods." If I try the following:
x = get(object,'ArrayIWant)
I get the error "Conversion to double from 'toolboxfunction' is not possible. How do get access to this array?!
Look for "Get" methods in the class:
methods(object)
or
methods className
Say it says there is a method called GetArrayIWant, then you'd do:
x = object.GetArrayIWant();
Hye guyz. I need an assistance with this. I have a figure of table. The user will insert a data into the table. If the user suddenly wrong insert the data, the table will be 'NaN'. My question is how i want to make the table does not display 'NaN' on the table but I want an error message appear. I have this coding:
function Mytable1_CreateFcn(hObject, eventdata, handles)
if isnan(Mytable1)
set(hObject, 'Data', 0);
errordlg('Input must be a number','Error');
end
handles.Mytable2 = hObject;
guidata(hObject,handles);
But there is an error with this code. Is this coding are correct to answer my question?
Update:
I did this coding on Mytable1_CellEditCallback. It still have error. Is this code true??
Mytable1=get(hObject,'Data')
if isnan(Mytable1)
set(hObject, 'Data', 0);
h=errordlg('Oh noes!','Error');
set(h, 'WindowStyle', 'modal');
uiwait(h);
return
end
handles.Mytable2 = hObject;
guidata(hObject,handles);
This is the error:
Mytable1 =
[1] [] []
[] [] []
[] [] []
[] [] []
??? Undefined function or method 'isnan' for input arguments of type 'cell'.
Error in ==> fyp_editor>Mytable1_CellEditCallback at 795
if ~isnan(Mytable1)
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> fyp_editor at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==>
#(hObject,eventdata)fyp_editor('Mytable1_CellEditCallback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uitable CellEditCallback
There are several errors in this code. Let me count the ways.
function Mytable1_CreateFcn(hObject, eventdata, handles)
The create function is executed at object creation, i.e. when the GUI is built. It is never executed otherwise, and thus it's a function you'll rarely ever want to modify. What you do want to modify is Mytable1_Callback
if isnan(Mytable1)
Mytable1 has not been defined within the function. Thus, this line will give you an error. Maybe you meant to add a line Mytable1=get(hObject,'Data')? Also, since Mytable1 is a cell array, you have to check the elements for NaNs using cellfun, i.e. write if any(cellfun(#isnan,Mytable1(:))).
set(hObject, 'Data', 0);
This line is most likely fine.
errordlg('Input must be a number','Error');
While this line is not an error, it won't behave as intended - The message pops up, but the function continues executing. Either you should write error('input must be a number), or write h=errordlg('Oh noes!');uiwait(h);return
end
Look, another line that is fine!
handles.Mytable2 = hObject; guidata(hObject,handles);
With this, you overwrite the handle to Mytable2 with the handle to Mytable1. Is that REALLY what you want?
if isnan(Mytable1)
Mytable is a cell array. Try this:
if any(isnan(cell2mat(Mytable1)))
However, there migth be an easier way, the Cell edit callback gets two inputs: the calling Object and the eventdata (and handles in guide). In the eventdata has following Fields:
Indices: 1-by-2 array containing the row and column indices of the cell the user edited.
PreviousData: Previous cell data. The default is an empty matrix, [].
EditData: User-entered string.
NewData: Value that MATLAB wrote to the Data property array. It is either the same as EditData or a converted value.
The NewData property is empty if MATLAB detects an error in the user-entered data.
Error: Error message returned if MATLAB detects an error in the user-entered data.
The Error property is empty when MATLAB successfully writes the value to the Data property.
If the Error property is not empty, then the CellEditCallback can display the string, or it can attempt to fix the problem.
Source:http://ch.mathworks.com/help/matlab/ref/uitable-properties.html#zmw57dd0e748724
With NewData or Error this should be possible to handle error checking.
for example:
MyTable_CellEditCallback(hObj,event,handles)
if isempty(event.NewData)
h=errordlg('Oh noes!','Error');
set(h, 'WindowStyle', 'modal');
uiwait(h);
return
end
% The rest of the function
end