R psych::statsBy() error: "'x' must be numeric" - correlation

I'm trying to do a multilevel factor analysis using the "psych" package. The first step is recommended to use the statsBy() funtion to have a correlation data:
statsBy(study2, group = "ID")
However, it gives this "Error in FUN(data[x, , drop = FALSE], ...) : 'x' must be numeric".
For the dataset, I only included a grouping variable "ID", and other two numeric variables. I ran the following line to check if the varibales are numeric.
sapply(study2, is.numeric)
ID v1 V2
FALSE TRUE TRUE
Here are the code in the tracedown of the error.But I don't know what 'x' refers here, and I noticed in line 8 and 9, the X is in captital and is lowercase in line 10.
*10.
FUN(data[x, , drop = FALSE], ...)
9.
FUN(X[[i]], ...)
8.
lapply(X = ans[index], FUN = FUN, ...)
7.
tapply(seq_len(728L), list(z = c("5edfa35e60122c277654d35b", "5ed69fbc0a53140e516ad4ed", "5d52e8160ebbe900196e252e", "5efa3da57a38f213146c7352", "5ef98f3df4d541726b1bcc48", "5debb7511e806c2a59cad664", "5c28a4530091e40001ca4d00", "5872a0d958ca4c00018ce4fe", "5c87868eddda2d00012add18", "5e80b7427567f07891655e7e", ...
6.
eval(substitute(tapply(seq_len(nd), IND, FUNx, simplify = simplify)), data)
5.
eval(substitute(tapply(seq_len(nd), IND, FUNx, simplify = simplify)), data)
4.
structure(eval(substitute(tapply(seq_len(nd), IND, FUNx, simplify = simplify)), data), call = match.call(), class = "by")
3.
by.data.frame(data, z, colMeans, na.rm = na.rm)
2.
by(data, z, colMeans, na.rm = na.rm)
1.
statsBy(study2, group = "ID")*
The dataset has 728 rows and those like "5edfa35e60122c277654d35b" are IDs. Could anyone help explain what might have gone wrong?

I had the same error, the only way was to convert the group variable to the numeric class.
Try:
study2$ID<-as.numeric(study2$ID)
statsBy(study2, group = "ID")
If dat$ID is of class character:
study2$ID<-as.numeric(as.factor(study2$ID))
statsBy(study2, group = "ID")

Related

Fine-tune a pre-trained model

I am new to transformer based models. I am trying to fine-tune the following model (https://huggingface.co/Chramer/remote-sensing-distilbert-cased) on my dataset. The code:
enter image description here
and I got the following error:
enter image description here
I will be thankful if anyone could help.
The preprocessing steps I followed:
input_ids_t = []
attention_masks_t = []
for sent in df_train['text_a']:
encoded_dict = tokenizer.encode_plus(
sent,
add_special_tokens = True,
max_length = 128,
pad_to_max_length = True,
return_attention_mask = True,
return_tensors = 'tf',
)
input_ids_t.append(encoded_dict['input_ids'])
attention_masks_t.append(encoded_dict['attention_mask'])
# Convert the lists into tensors.
input_ids_t = tf.concat(input_ids_t, axis=0)
attention_masks_t = tf.concat(attention_masks_t, axis=0)
labels_t = np.asarray(df_train['label'])
and i did the same for testing data. Then:
train_data = tf.data.Dataset.from_tensor_slices((input_ids_t,attention_masks_t,labels_t))
and the same for testing data
It sounds like you are feeding the transformer_model 1 input instead of 3. Try removing the square brackets around transformer_model([input_ids, input_mask, segment_ids])[0] so that it reads transformer_model(input_ids, input_mask, segment_ids)[0]. That way, the function will have 3 arguments and not just 1.

Divisor is equal to zero, Need guidance with case statement

This is my query & I'm getting error divisor is equal to zero, I know i need to build this as a case statement just tried a few things and cant get it to work, thanks in advance.
NVL(ROUND(((SELECT PC.BUCKET_ACCUM_COST
FROM PART_CB PC WHERE
PART_CB_NO = '201'
AND PC.PART_NO = I.PART_NO
AND
PC.CONTRACT = P.CONTRACT
AND
PC.TOP_LEVEL_PART_NO || '' =
Z_BEL_FINANCE_API.GET_PART_COST_TOP_PART_NO(P.CONTRACT, P.PART_NO,
P.COST_SET, P.ALTERNATIVE_NO,
P.ROUTING_ALTERNATIVE_NO)
AND
PC.COST_SET = P.COST_SET
AND
PC.COST_BUCKET_ID != 'SYS'
AND
PC.TOP_ALTERNATIVE_NO =
Z_BEL_FINANCE_API.GET_PART_COST_TOP_ALT_NO(P.CONTRACT, P.PART_NO,
P.COST_SET, P.ALTERNATIVE_NO,
P.ROUTING_ALTERNATIVE_NO)
AND
PC.TOP_ROUTING_NO =
Z_BEL_FINANCE_API.GET_PART_COST_TOP_ROUTING_NO(P.CONTRACT, P.PART_NO,
P.COST_SET, P.ALTERNATIVE_NO,
P.ROUTING_ALTERNATIVE_NO)
AND
PC.BUCKET_SEQ = Z_BEL_FINANCE_API.GET_PART_COST_BUCKET_SEQ(P.CONTRACT,
P.PART_NO, P.COST_SET, P.ALTERNATIVE_NO,
P.ROUTING_ALTERNATIVE_NO)) /
(SELECT WC_RATE
FROM WCT
WHERE WORK_CENTER_NO = 'COST1'
AND COST_SET = '1'
AND CONTRACT = P.CONTRACT)), 4), 0) MACHINE_SETUP_TIME,
The only dividing in this mess is here:
/ (SELECT WC_RATE FROM WCT ...)
If you don't want to divide with zero, you'll have to handle it.
For example, use DECODE (or CASE) and - if you want to get 0 as the result, divide with a very large number (e.g. 1E99)

Modifying a specific part of the name of a DT in R

I have the following DT
structure(list(HKU47_PSG_1_HW_0.txt = 66611.1718226969, HKU47_PSG_1_HW_1.txt = 66254.5524579138,
HKU47_PSG_1_HW_2.txt = 66972.3593176305, HKU47_PSG_1_HW_3.txt = 68419.8681965619,
HKU47_PSG_1_HW_4.txt = 66841.3761239946, HKU47_PSG_1_HW_5.txt = 66196.5383069813), .Names = c("HKU47_PSG_1_HW_0.txt",
"HKU47_PSG_1_HW_1.txt", "HKU47_PSG_1_HW_2.txt", "HKU47_PSG_1_HW_3.txt",
"HKU47_PSG_1_HW_4.txt", "HKU47_PSG_1_HW_5.txt"), row.names = c(NA,
-1L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x0000000000100788>)
and I would like to have a clone of this DT cancelling from each column name this part of text HW_ and .txt. I would need something like names(data) <- c("new_name", "another_new_name") which works automatically for several DT I have. I do not have actually a clear idea how to do that.
You can use sub to replace HW_ with empty string, and replace .txt with empty string, effectively removing those parts from the names:
names(data) <- sub('HW_', '', sub('\\.txt', '', names(data)))

Is there something wrong with this xpath, or is Marklogic misbehaving?

I've got a weird problem with what should be a simple xpath not returning data when I'm sure the data is present in Marklogic. I can see the data in question through a more general xpath, but not get it specifically.
xpath that works:
/log:record[log:changes/log:change/#field = "moduleCodes"]
=> a long sequence of log:record elements with "moduleCodes" field changes
xpath that doesn't:
/log:record/log:changes/log:change[#field = "moduleCodes"]
=> empty sequence
(I've ommitted the log namespace definition brevity.)
Trying to figure out what's going on, I started with the first, working, xpath and built on it:
/log:record[log:changes/log:change/#field = "moduleCodes"]/log:changes/log:change
=> sequence of log:change elements including some with #field = "moduleCodes"
/log:record[log:changes/log:change/#field = "moduleCodes"]/log:changes/log:change[#field]
=> sequence of log:change elements including some with #field = "moduleCodes"
/log:record[log:changes/log:change/#field = "moduleCodes"]/log:changes/log:change[#field = "moduleCodes"]
=> empty sequence
Am I misunderstanding something fundamental? I can't see any reason why the xpaths putting the predicate on the log:change would return an empty sequence when everything else works as I expect. This feels like Marklogic getting confused somehow to me, but I want to make sure it's not just me missing a subtlety of xpath before I start talking like that.
I just tried the paths with a different field-name. It works as I expect with (at least some) other values.
I just restarted the ML cluster; no change.
Edit:
All of the xpaths above work fine in Oxygen, so it seems to be just ML that's behaving like this. I tried adding fn:doc() to the front of all the paths, in case that helped, but it made no difference.
Here's an (anonymised) record that I believe should match all the xpaths:
<log:record id="00000001" date="2013-04-14T01:42:02.922+01:00" type="change" xmlns:log="some/namespace/definition">
<log:head>
<some-header-info/>
</log:head>
<log:changes>
<log:change field="dateModified">
<log:old-value>2012-11-06T00:00:00.0000000</log:old-value>
<log:new-value>2013-03-20T00:00:00.0000000</log:new-value>
</log:change>
<log:change field="moduleCodes">
<log:old-value>
<log:moduleCodes>
<log:moduleCodes-value code="AAA"/>
</log:moduleCodes>
</log:old-value>
<log:new-value>
<log:moduleCodes>
<log:moduleCodes-value code="AAA"/>
<log:moduleCodes-value code="BBB"/>
</log:moduleCodes>
</log:new-value>
</log:change>
</log:changes>
</log:record>
As best I can recreate your test with 6.0-2.3, this works for me.
When debugging database queries, one useful technique is to move things in memory. If it still doesn't work, this throws suspicion on the database query. When I try that using 6.0-2.3, the results seem to be correct.
declare namespace log="some/namespace/definition" ;
document {
<log:record id="00000001" date="2013-04-14T01:42:02.922+01:00" type="change" xmlns:log="some/namespace/definition">
<log:head>
<some-header-info/>
</log:head>
<log:changes>
<log:change field="dateModified">
<log:old-value>2012-11-06T00:00:00.0000000</log:old-value>
<log:new-value>2013-03-20T00:00:00.0000000</log:new-value>
</log:change>
<log:change field="moduleCodes">
<log:old-value>
<log:moduleCodes>
<log:moduleCodes-value code="AAA"/>
</log:moduleCodes>
</log:old-value>
<log:new-value>
<log:moduleCodes>
<log:moduleCodes-value code="AAA"/>
<log:moduleCodes-value code="BBB"/>
</log:moduleCodes>
</log:new-value>
</log:change>
</log:changes>
</log:record> }
/log:record[log:changes/log:change/#field = "moduleCodes"]/xdmp:path(.)
=>
/log:record
declare namespace log="some/namespace/definition" ;
document {
<log:record id="00000001" date="2013-04-14T01:42:02.922+01:00" type="change" xmlns:log="some/namespace/definition">
<log:head>
<some-header-info/>
</log:head>
<log:changes>
<log:change field="dateModified">
<log:old-value>2012-11-06T00:00:00.0000000</log:old-value>
<log:new-value>2013-03-20T00:00:00.0000000</log:new-value>
</log:change>
<log:change field="moduleCodes">
<log:old-value>
<log:moduleCodes>
<log:moduleCodes-value code="AAA"/>
</log:moduleCodes>
</log:old-value>
<log:new-value>
<log:moduleCodes>
<log:moduleCodes-value code="AAA"/>
<log:moduleCodes-value code="BBB"/>
</log:moduleCodes>
</log:new-value>
</log:change>
</log:changes>
</log:record> }
/log:record/log:changes/log:change[#field = "moduleCodes"]/xdmp:path(.)
=>
/log:record/log:changes/log:change[2]
So the implication is that the problem is in the index or the way the index is queried. You can try to debug that using xdmp:query-trace(true()) at the start of your query. For example:
declare namespace log="some/namespace/definition" ;
xdmp:query-trace(true()),
/log:record[log:changes/log:change/#field = "moduleCodes"]/xdmp:describe(.),
/log:record/log:changes/log:change[#field = "moduleCodes"]/xdmp:describe(.)
With 6.0-2.3 these both return the expected results for me.
fn:doc("test")/log:record
fn:doc("test")/log:record/log:changes/log:change[2]
Here are the traces, from the ErrorLog.txt file:
Analyzing path: fn:collection()/log:record[log:changes/log:change/#field = "moduleCodes"]/xdmp:describe(.)
Step 1 is searchable: fn:collection()
Step 2 is searchable: log:record[log:changes/log:change/#field = "moduleCodes"]
Step 3 is unsearchable: xdmp:describe(.)
First 2 steps of path are searchable: fn:collection()/log:record[log:changes/log:change/#field = "moduleCodes"]
Gathering constraints.
Comparison contributed hash value constraint: log:change/#field = "moduleCodes"
Step 2 predicate 1 contributed 3 constraints: log:changes/log:change/#field = "moduleCodes"
Comparison contributed hash value constraint: log:change/#field = "moduleCodes"
Step 2 predicate 1 contributed 1 constraint: log:changes/log:change/#field = "moduleCodes"
Step 2 contributed 4 constraints: log:record[log:changes/log:change/#field = "moduleCodes"]
Executing search.
Selected 1 fragment to filter
xdmp:eval("declare namespace log="some/namespace/definition" ;&#1...", (), <options xmlns="xdmp:eval"><database>598453498912235799</database><root>/tmp</root><isolati...</options>)
Analyzing path: fn:collection()/log:record/log:changes/log:change[#field = "moduleCodes"]/xdmp:describe(.)
Step 1 is searchable: fn:collection()
Step 2 is searchable: log:record
Step 3 is searchable: log:changes
Step 4 is searchable: log:change[#field = "moduleCodes"]
Step 5 is unsearchable: xdmp:describe(.)
First 4 steps of path are searchable: fn:collection()/log:record/log:changes/log:change[#field = "moduleCodes"]
Gathering constraints.
Step 2 contributed 1 constraint: log:record
Comparison contributed hash value constraint: log:change/#field = "moduleCodes"
Step 4 predicate 1 contributed 1 constraint: #field = "moduleCodes"
Step 4 contributed 1 constraint: log:change[#field = "moduleCodes"]
Comparison contributed hash value constraint: log:change/#field = "moduleCodes"
Step 4 predicate 1 contributed 1 constraint: #field = "moduleCodes"
Comparison contributed hash value constraint: log:change/#field = "moduleCodes"
Step 4 predicate 1 contributed 1 constraint: #field = "moduleCodes"
Step 4 contributed 1 constraint: log:change[#field = "moduleCodes"]
Step 3 contributed 1 constraint: log:changes
Executing search.
Selected 1 fragment to filter

Lua: how use all tables in table

positions = {
--table 1
[1] = {pos = {fromPosition = {x=1809, y=317, z=8},toPosition = {x=1818, y=331, z=8}}, m = {"100 monster"}},
--table 2
[2] = {pos = {fromPosition = {x=1809, y=317, z=8},toPosition = {x=1818, y=331, z=8}}, m = {"100 monster"}},
-- table3
[3] = {pos = {fromPosition = {x=1809, y=317, z=8},toPosition = {x=1818, y=331, z=8}}, m = {"100 monster"}}
}
tb = positions[?]--what need place here?
for _,x in pairs(tb.m) do --function
for s = 1, tonumber(x:match("%d+")) do
pos = {x = math.random(tb.pos.fromPosition.x, tb.pos.toPosition.x), y = math.random(tb.pos.fromPosition.y, tb1.pos.toPosition.y), z = tb.pos.fromPosition.z}
doCreateMonster(x:match("%s(.+)"), pos)
end
end
Here the problem, i use tb = positions[1], and it only for one table in "positions" table. But how apply this function for all tables in this table?
I don't know Lua very well but you could loop over the table:
for i = 0, table.getn(positions), 1 do
tb = positions[i]
...
end
Sources :
http://lua.gts-stolberg.de/en/schleifen.php and http://www.lua.org/pil/19.1.html
You need to iterate over positions with a numerical for.
Note that, unlike Antoine Lassauzay's answer, the loop starts at 1 and not 0, and uses the # operator instead of table.getn (deprecated function in Lua 5.1, removed in Lua 5.2).
for i=1,#positions do
tb = positions[i]
...
end
use the pairs() built-in. there isn't any reason to do a numeric for loop here.
for index, position in pairs(positions) do
tb = positions[index]
-- tb is now exactly the same value as variable 'position'
end

Resources