Accuracy.meas function in ROSE package of R - r

I am using accuracy.meas function of ROSE package in R. I got the error Response must have two levels. So checked
both the parameter response and predicted1. But both are numeric. Is there some limitations to usability of accuracy.meas function.
Note- The answer is wrong but it has nothing to do with error
accuracy.meas(test$Walc,predicted1,threshold = 0.5)
Error in accuracy.meas(response=test$Walc,predicted= predicted1, threshold = 0.5) :
Response must have two levels.
>test$Walc
[1] 1 1 1 3 3 3 1 1 2 2 1 2 1 1 3 3 1 1 1 1 3 1 1 4 2 1 1 1 1 4 4 4 5 1 1 1 1 3 1 2 3
[42] 1 5 1 4 4 1 2 2 2 1 2 2 3 2 3 1 2 1 5 1 1 3 2 2 1 1 1 1 1 1 1 2 1 1 3 3 3 2 3 1 2
[83] 2 2 1 1 3 1 1 1 2 3 3 1 1 3 1 2 1 5 2 2 1 2 1 1 2 2 1 1 3 1 2 1 1 1 3 1 1 1 1 1 1
[124] 3 3 3 4 1 1 1 1 4 1 1 1 1 3 2 1 3 3 1 1 1 1 1 1 1 1 5 1 1 1 3 1 1 1 3 4 1 3 2 4 5
[165] 2 1 1 2 1 1 2 3 1 4 1 2 1 4 4 5 1 1 5 3 5 4 5 2 4 2 2 4 1 5 5 4 2 2 1 4 4 4 2 3 4
[206] 2 3 4 4 5 2 3 4 5 5 3 2 4 4 1 5 5 5 3 2 2 4 1 5 5 2 1 1 1 2 3 3 2 1 1 3 4 1 1 1 4
[247] 1 3 1 2 2 3 3 2 2 2 2 1 2 1 1 1 1 3 1 1 1 1 1 1 1 2 1 1 3 1 1 4 3 5 2 2 4 3 4 2 3
[288] 5 5 3 1 1 3 4 4 4 3 4 5 3 3 3 3 3 4 4 3 1 3 3 4 3
> predicted1
[1] 2 2 1 2 2 2 1 1 1 2 2 2 1 1 4 4 1 1 1 1 3 2 2 3 2 2 1 2 2 2 2 2 5 3 3 2 2 2 1 1 2
[42] 1 3 2 3 3 2 2 2 2 2 2 2 3 1 3 2 1 2 4 2 3 2 3 3 1 2 2 2 1 1 2 2 1 1 2 2 3 1 2 2 2
[83] 2 2 1 1 3 2 2 1 1 3 3 1 2 2 2 3 1 3 3 3 1 2 1 2 1 2 3 1 3 2 2 2 2 2 2 2 2 2 2 1 2
[124] 4 1 4 4 2 1 1 2 1 1 2 1 1 2 2 2 3 3 1 1 1 1 2 1 1 1 4 2 1 1 2 2 1 2 2 3 1 2 2 3 4
[165] 2 2 2 3 2 1 2 2 2 4 1 2 2 4 4 5 1 1 5 2 5 4 4 2 4 3 2 2 1 4 4 2 2 2 1 4 2 3 2 3 4
[206] 3 2 4 4 5 2 2 4 4 5 4 3 3 3 2 4 4 4 3 1 2 2 2 4 4 1 1 2 2 2 3 3 1 2 1 2 2 1 1 3 2
[247] 2 2 1 4 2 2 4 2 2 2 2 2 2 2 1 1 3 2 1 2 2 2 2 1 1 2 2 2 4 4 2 3 3 5 2 2 3 3 3 3 3
[288] 3 5 4 2 2 4 4 5 4 3 4 5 3 4 4 3 3 3 3 3 2 4 4 2 3

Related

Stratified Fisher and Wilcoxon test

I would like to perform a stratified fisher test.
I've tried with tabulate without success.
These are my data:
> db$site
[1] 2 2 2 3 1 1 1 1 1 1 1 1 1 1 2 3 1 1 1 1 1 1 1 1 2 2 3 3 1 1 3 1 2 1 1 2 1 1 1 1 1 3 1 1 1 1 1 3 1
[50] 2 1 1 3 3 3 1 1 1 1 1 1 1 1 1 1 1 1 1 3 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 1 3 3 1
[99] 3 1 1 1 1 1 1 1 3 3 1 1 1 1 1 3 2 1 1 1 1 1 3 3 1 3 3 3 3 1 3 1 3 3 1 3 1 1 3 3 3 2 3 3 3 3 1 3 3
[148] 3 2 3 3 1 3 1 3 3 3 3 3 3 1 3 3 3 1 3 3 1 3 1 1 1 1 1 1 1 3 2 1 3 2 2 2 3 2 3 2 2 2 2 2 2 2 2 3 2
[197] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 3 1 3 2 1 2 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3 1 1
[344] 2 1 1 2 2 2 1 4 2 2 1 4 1 1 4 2 1 4 2 3 2 1 1
Levels: 1 2 3 4 5
> db$area
[1] 3 1 1 0 3 2 5 3 1 3 3 3 1 5 4 0 3 5 5
[20] 1 3 2 3 1 3 3 0 0 3 3 0 1 3 1 3 3 3 3
[39] 3 3 3 0 1 2 2 2 2 0 2 1 1 1 0 0 0 3 3
[58] 3 3 4 3 3 3 3 3 1 3 3 0 3 3 5 3 2 3 5
[77] 5 3 3 3 3 5 3 2 5 3 3 3 2 0 0 0 0 0 5
[96] 0 0 3 0 3 5 3 3 3 1 3 0 0 3 3 2 1 3 0
[115] 3 2 5 2 5 1 0 0 5 0 0 0 0 1 0 1 0 0 3
[134] 0 3 3 0 0 0 3 0 0 0 0 3 0 0 0 3 0 0 2
[153] 0 3 0 0 0 0 0 0 3 0 0 0 3 0 0 3 0 5 3
[172] 5 3 3 3 3 0 2 3 0 3 2 3 0 3 0 3 2 5 3
[191] 2 3 5 5 0 3 5 3 2 3 3 3 3 2 2 3 1 3 3
[210] 3 3 3 5 3 3 3 3 3 0 3 0 3 2 1 0 3 0 0
[229] 5 3 3 1 0 0 0 3 0 5 1 3 0 3 3 0 1 5 5
[248] 3 1 3 5 0 2 3 2 0 0 0 3 3 5 0 3 0 0 0
[267] 3 3 3 0 3 0 3 4 0 0 3 3 3 3 5 5 3 3 1
[286] 3 1 2 3 0 0 5 3 1 0 5 3 0 3 3 3 2 3 0
[305] 0 <NA> 3 0 3 5 5 0 5 3 0 3 1 0 5 3 3 3 2
[324] <NA> 0 3 5 1 0 0 0 5 0 5 0 5 0 3 3 3 0 0
[343] 0 0 2 3 3 3 0 5 0 3 3 0 3 0 5 1 0 3 3
[362] 2 3 3 3 3
Levels: 0 1 2 3 4 5
library(survival)
library(Exact)
library(plyr)
b<-tabulate(db$site, db$phq_cat, db$area, tests=c("fisher"))
I obtain this error message:
Error in tabulate(db$site, db$phq_cat, db$area, tests = c("fisher")) :
unused arguments (db$AREEDISCIPL, tests = c("fisher"))
How cain I handle this?
I also would like to perform stratified wilcoxon rank sum test.
Is there a way?
Thank you!

When I run complete(), I am getting the an error- Error in (function (classes, fdef, mtable)

I have a dataset which looks like this(A-J are column names)
A B C D E F G H I J
1 2 2 3 2 1 1 1 1
2 1 1 1 1 1 1 1 1 1
2 1 2 2 2 2 2 2 1 1
2 1 2 1 1 1 1 1 1 1
2 1 3 3 3 2 2 2 2
2 1 3 2 2 3 1 1 1 1
1 3 2 1 2 2 2 1 2
2 1 2 2 2 2 2 2 1 1
1 2 2 2 2 1 1 1 1
2 1 2 1 1 1 2 1 1 1
2 1 1 1 1 1 2 2 1 1
2 1 2 1 1 1 1 1 1 2
2 1 1 1 1 1 1 1
2 1 3 3 3 3 1 1 1 2
1 2 2 1 2 1 1 1 1
1 2 2 2 2 2 2 1 1
2 2 4 1 1 1 2 2 1 1
1 1 3 3 3 3
2 1 3 3 1 2 2 2 2 3
I am getting the below error-
Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘complete’ for signature ‘"mids", "numeric"’
My data has lot of NULL values and I am trying to impute the data using below code-
imp_data<-mice(data = data_NA, m = 5, method = "rf", maxit = 5, seed = 500)
I get the error when I run the code-
complete(imp_data,1)
Please suggest where I am doing wrong
It seems that NA values are not properly assigned in the data_NA data.frame which is causing the problem.
The modified data (with NA) and transforming it using mice as it worked for me:
library(mice)
imp_data <- mice(data = data_NA, m = 5, method = "rf", maxit = 5, seed = 500)
complete(imp_data, 1)
EDITED: The error seen by OP was resolved by changing the call as:
mice::complete(imp_data, 1)
May be the mice::complete was masked by some function other package.
#Result
# A B C D E F G H I J
# 1 1 2 2 3 2 1 1 1 1 2
# 2 2 1 1 1 1 1 1 1 1 1
# 3 2 1 2 2 2 2 2 2 1 1
# 4 2 1 2 1 1 1 1 1 1 1
# 5 2 1 1 3 3 3 2 2 2 2
# 6 2 1 3 2 2 3 1 1 1 1
# 7 1 3 2 1 2 2 2 1 2 1
# 8 2 1 2 2 2 2 2 2 1 1
# 9 1 2 2 2 2 1 1 1 1 1
# 10 2 1 2 1 1 1 2 1 1 1
# 11 2 1 1 1 1 1 2 2 1 1
# 12 2 1 2 1 1 1 1 1 1 2
# 13 2 1 1 1 1 1 1 1 1 1
# 14 2 1 3 3 3 3 1 1 1 2
# 15 1 2 2 1 2 1 1 1 1 1
# 16 1 2 2 2 2 2 2 1 1 1
# 17 2 2 4 1 1 1 2 2 1 1
# 18 1 1 3 3 3 3 2 1 2 1
# 19 2 1 3 3 1 2 2 2 2 3
#
Data
data_NA<- read.table(text =
"A B C D E F G H I J
1 2 2 3 2 1 1 1 1 NA
2 1 1 1 1 1 1 1 1 1
2 1 2 2 2 2 2 2 1 1
2 1 2 1 1 1 1 1 1 1
2 1 NA 3 3 3 2 2 2 2
2 1 3 2 2 3 1 1 1 1
1 3 2 1 2 2 2 1 2 NA
2 1 2 2 2 2 2 2 1 1
1 2 2 2 2 1 1 1 1 NA
2 1 2 1 1 1 2 1 1 1
2 1 1 1 1 1 2 2 1 1
2 1 2 1 1 1 1 1 1 2
2 1 1 1 NA NA 1 1 1 1
2 1 3 3 3 3 1 1 1 2
1 2 2 1 2 1 1 1 1 NA
1 2 2 2 2 2 2 1 1 NA
2 2 4 1 1 1 2 2 1 1
1 1 3 3 3 3 NA NA NA NA
2 1 3 3 1 2 2 2 2 3",header = TRUE)

select columns by rule and stack them in R

Area RA RI WA WI NA NI
3 3 1 4 2 2 1
2 2 1 3 1 2 1
3 2 1 3 2 2 1
2 2 1 3 1 1 1
2 2 1 3 2 1 1
2 2 1 2 1 2 1
2 3 1 2 1 2 1
3 1 1 2 2 1 1
2 2 1 1 1 2 1
2 2 1 2 1 2 1
3 1 1 3 1 1 1
I want to retain the first column and stack every two columns as:
Area columan 1 Column 2
3 3 1
2 2 1
3 2 1
2 2 1
2 2 1
2 2 1
2 3 1
3 1 1
2 2 1
2 2 1
3 1 1
3 4 2
2 3 1
3 3 2
2 3 1
2 3 2
2 2 1
2 2 1
3 2 2
2 1 1
2 2 1
3 3 1
3 2 1
2 2 1
3 2 1
2 1 1
2 1 1
2 2 1
2 2 1
3 1 1
2 2 1
2 2 1
3 1 1
Your suggest highly appreciated !
We get the alternating columns using the recycling logical index (c(TRUE, FALSE) after subsetting the dataset without the first column (df1[-1]), unlist and cbind it with the first column.
d1 <- data.frame(Area = df1[,1], column1 = unlist(df1[-1][ c(TRUE, FALSE)]),
column2 = unlist(df1[-1][c(FALSE, TRUE)]))
row.names(d1) <- NULL
head(d1)
# Area column1 column2
#1 3 3 1
#2 2 2 1
#3 3 2 1
#4 2 2 1
#5 2 2 1
#6 2 2 1
tail(d1)
# Area column1 column2
#28 2 2 1
#29 2 2 1
#30 3 1 1
#31 2 2 1
#32 2 2 1
#33 3 1 1

Using "ward" method with pvclust in R

I am using the pvclust package in R to get hierarchical clustering dendrograms with p-values.
I want to use the "Ward" clustering and the "Euclidean" distance method. Both work fine with my data when using hclust. In pvclust however I keep getting the error message "invalid clustering method". The problem apparently results from the "ward" method, because other methods such as "average" work fine, as does "euclidean" on its own.
This is my syntax and the resulting error message:
result <- pvclust(t(data2007num), method.hclust="ward", method.dist="euclidean", nboot=100)
Bootstrap (r = 0.5)...
Error in hclust(distance, method = method.hclust) : invalid clustering method
My data matrix has the following form (28 countries x 20 policy dimensions):
X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 Y1 Y2 Y3 Y4 Y5 Y6 Y7 Y8 Y9 Y10
AUT 2 3 4 2 1 1 4 3 2 2 2 3 3 4 4 2.0 5 4 0 3
GER 3 5 3 2 1 3 2 4 4 5 4 0 4 5 4 3.0 5 5 3 2
SWE 5 5 1 5 4 3 1 4 4 5 3 4 5 2 4 3.0 3 3 5 0
NLD 4 4 2 3 2 1 0 4 4 0 4 4 4 2 2 4.0 4 4 2 5
ESP 3 4 1 4 5 0 3 2 4 1 4 3 3 1 2 3.0 2 2 0 2
ITA 3 2 0 3 1 1 3 3 5 5 4 2 4 1 1 2.0 0 2 0 2
FRA 3 2 1 3 1 2 4 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provided in R (such as Boston in the MASS package, without any success. Does anyone now a solution or if the "ward" method was disabled inpvclust?

Off-diagonal and Diagonal symmetry check, Getting off-diagonal and diagonal element(s) without repetition of a Matrix

Suppose I have this matrix
8 3 1 1 2 2 1 1 1 1 1 1 2 2 1 1 3
3 8 3 1 1 2 2 1 1 1 1 1 1 2 2 1 1
1 3 8 3 1 1 2 2 1 1 1 1 1 1 2 2 1
1 1 3 8 3 1 1 2 2 1 1 1 1 1 1 2 2
2 1 1 3 8 3 1 1 2 2 1 1 1 1 1 1 2
2 2 1 1 3 8 3 1 1 2 2 1 1 1 1 1 1
1 2 2 1 1 3 8 3 1 1 2 2 1 1 1 1 1
1 1 2 2 1 1 3 8 3 1 1 2 2 1 1 1 1
1 1 1 2 2 1 1 3 8 3 1 1 2 2 1 1 1
1 1 1 1 2 2 1 1 3 8 3 1 1 2 2 1 1
1 1 1 1 1 2 2 1 1 3 8 3 1 1 2 2 1
1 1 1 1 1 1 2 2 1 1 3 8 3 1 1 2 2
2 1 1 1 1 1 1 2 2 1 1 3 8 3 1 1 2
2 2 1 1 1 1 1 1 2 2 1 1 3 8 3 1 1
1 2 2 1 1 1 1 1 1 2 2 1 1 3 8 3 1
1 1 2 2 1 1 1 1 1 1 2 2 1 1 3 8 3
3 1 1 2 2 1 1 1 1 1 1 2 2 1 1 3 8
I want to check
Off-diagonals are symmetric or not?(in above matrix, these are symmetric)
Elements occur in Off-diagonal (without repetition)?-- in above matrix, these elements are 1,2,3
Elements in diagonal are symmetric? if yes print element? (like 8 in above matrix)
# 1
all(mat == t(mat))
[1] TRUE
# 2
unique(mat[upper.tri(mat) | lower.tri(mat)])
[1] 3 1 2
# 3
if(length(unique(diag(mat))) == 1) print(diag(mat)[1])
[1] 8
mat <- as.matrix(read.table('abbas.txt'))
isSymmetric(unname(mat))
'Note that a matrix is only symmetric if its 'rownames' and 'colnames' are identical.'
unique(mat[lower.tri(mat)])
all(diag(mat) == rev(diag(mat)))
# I assume you mean the diagonal is symmetric when its reverse is the same with itself.

Resources