Re: [R] expand a matrix

2022-09-05 Thread Andrew Simmons
You can specify multiple indexes to replace at once, so you can avoid
a for loop entirely like this:
M <- matrix(nrow = 10, ncol = 10)
M[1:4, 5: 6] <- M[5: 6, 1:4] <- m[1, 2]
M[1:4,7] <- M[   7, 1:4] <- m[1, 3]
M[1:4, 8:10] <- M[8:10, 1:4] <- m[1, 4]
M[5:6,7] <- M[   7, 5:6] <- m[2, 3]
M[5:6, 8:10] <- M[8:10, 5:6] <- m[2, 4]
M[  7, 8:10] <- M[8:10,   7] <- m[3, 4]
M


You could also specify it directly with function 'matrix', it is
somewhat ugly though:
M <- matrix(c(
NA, NA, NA, NA, m[1,2], m[1,2], m[1,3], m[1,4],
m[1,4], m[1,4],
NA, NA, NA, NA, m[1,2], m[1,2], m[1,3], m[1,4],
m[1,4], m[1,4],
NA, NA, NA, NA, m[1,2], m[1,2], m[1,3], m[1,4],
m[1,4], m[1,4],
NA, NA, NA, NA, m[1,2], m[1,2], m[1,3], m[1,4],
m[1,4], m[1,4],
m[1,2], m[1,2], m[1,2], m[1,2], NA, NA, m[2,3], m[2,4],
m[2,4], m[2,4],
m[1,2], m[1,2], m[1,2], m[1,2], NA, NA, m[2,3], m[2,4],
m[2,4], m[2,4],
m[1,3], m[1,3], m[1,3], m[1,3], m[2,3], m[2,3], NA, m[3,4],
m[3,4], m[3,4],
m[1,4], m[1,4], m[1,4], m[1,4], m[2,4], m[2,4], m[3,4], NA,
 NA, NA,
m[1,4], m[1,4], m[1,4], m[1,4], m[2,4], m[2,4], m[3,4], NA,
 NA, NA,
m[1,4], m[1,4], m[1,4], m[1,4], m[2,4], m[2,4], m[3,4], NA,
 NA, NA
), nrow = 10, ncol = 10, byrow = TRUE)
M


If it interests you, you could take a look at package 'Matrix', it
contains classes for specific types of matrices, including upper
triangular matrices. This package helps to improve speed of linear
algebra computations and reduce the memory size of matrices.

On Mon, Sep 5, 2022 at 10:46 PM Jinsong Zhao  wrote:
>
> Hi there,
>
> I have a matrix likes:
>  > m
>   [,1] [,2] [,3] [,4]
> [1,]   NA123
> [2,]1   NA65
> [3,]26   NA4
> [4,]354   NA
>
> I hope to expand it to 10 by 10 matrix, M, likes:
>  > M
>[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
>   [1,]   NA   NA   NA   NA11233 3
>   [2,]   NA   NA   NA   NA11233 3
>   [3,]   NA   NA   NA   NA11233 3
>   [4,]   NA   NA   NA   NA11233 3
>   [5,]1111   NA   NA655 5
>   [6,]1111   NA   NA655 5
>   [7,]222266   NA44 4
>   [8,]3333554   NA   NANA
>   [9,]3333554   NA   NANA
> [10,]3333554   NA   NANA
>
> I use the following code:
>
> M <- matrix(NA, 10, 10)
>
> for (i in 1:10) {
> for (j in 1:10) {
>if (i %in% 1:4 & j %in% 5:6) M[i,j] <- M[j,i] <- m[1,2]
>if (i %in% 1:4 & j == 7) M[i,j] <- M[j,i] <-m[1,3]
>if (i %in% 1:4 & j %in% 8:10) M[i,j] <- M[j,i] <-m[1,4]
>if (i %in% 5:6 & j == 7) M[i,j] <- M[j,i] <-m[2,3]
>if (i %in% 5:6 & j %in% 8:10) M[i,j] <- M[j,i] <-m[2,4]
>if (i == 7 & j %in% 8:10) M[i,j] <- M[j,i] <-m[3,4]
> }
> }
>
> Is there any convenience way to do it? Thanks!
>
> Best,
> Jinsong
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] expand a matrix

2022-09-05 Thread Jinsong Zhao

Hi there,

I have a matrix likes:
> m
 [,1] [,2] [,3] [,4]
[1,]   NA123
[2,]1   NA65
[3,]26   NA4
[4,]354   NA

I hope to expand it to 10 by 10 matrix, M, likes:
> M
  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]   NA   NA   NA   NA11233 3
 [2,]   NA   NA   NA   NA11233 3
 [3,]   NA   NA   NA   NA11233 3
 [4,]   NA   NA   NA   NA11233 3
 [5,]1111   NA   NA655 5
 [6,]1111   NA   NA655 5
 [7,]222266   NA44 4
 [8,]3333554   NA   NANA
 [9,]3333554   NA   NANA
[10,]3333554   NA   NANA

I use the following code:

M <- matrix(NA, 10, 10)

for (i in 1:10) {
   for (j in 1:10) {
  if (i %in% 1:4 & j %in% 5:6) M[i,j] <- M[j,i] <- m[1,2]
  if (i %in% 1:4 & j == 7) M[i,j] <- M[j,i] <-m[1,3]
  if (i %in% 1:4 & j %in% 8:10) M[i,j] <- M[j,i] <-m[1,4]
  if (i %in% 5:6 & j == 7) M[i,j] <- M[j,i] <-m[2,3]
  if (i %in% 5:6 & j %in% 8:10) M[i,j] <- M[j,i] <-m[2,4]
  if (i == 7 & j %in% 8:10) M[i,j] <- M[j,i] <-m[3,4]
   }
}

Is there any convenience way to do it? Thanks!

Best,
Jinsong

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Clustering of datasets

2022-09-05 Thread Rui Barradas

Hello,

I am not at all sure that the following answers the question.
The code below ries to find the optimal number of clusters. One of the 
changes I have made to your call to kmeans is to subset DMs not dropping 
the dim attribute.



library(cluster)

max_clust <- 10
wss <- numeric(max_clust)

for(k in 1:max_clust) {
  km <- kmeans(DMs[,2], centers = k, nstart = 25)
  wss[k] <- km$tot.withinss
}
plot(wss, type = "b")

dm <- DMs[, 2, drop = FALSE]
# Where is the elbow, at 2 or at 4?
factoextra::fviz_nbclust(dm, kmeans, method = "wss")
factoextra::fviz_nbclust(dm, kmeans, method = "silhouette")

k2 <- kmeans(dm, centers = 2, nstart = 25)
k3 <- kmeans(dm, centers = 3, nstart = 25)
k4 <- kmeans(dm, centers = 4, nstart = 25)

main2 <- paste(length(k2$centers), "clusters")
main3 <- paste(length(k3$centers), "clusters")
main4 <- paste(length(k4$centers), "clusters")

old_par <- par(mfcol = c(1, 3))
plot(DMs[,2], col = k2$cluster, pch = 19, main = main2)
plot(DMs[,2], col = k3$cluster, pch = 19, main = main3)
plot(DMs[,2], col = k4$cluster, pch = 19, main = main4)
par(old_par)


Hope this helps,

Rui Barradas


Às 12:31 de 05/09/2022, Subhamitra Patra escreveu:

Dear all,

I am about to cluster my datasets by using K-mean clustering techniques in
R, but getting some type of scattered results. Herewith I pasted my code
below. Please suggest to me where I am lacking in my code. I was pasting my
data before applying the K-mean method as follows.

DMs<-read.table(text="Country DATA
   IS -0.0092
   BA -0.0235
   HK -0.0239
   JA -0.0333
   KU -0.0022
   OM -0.0963
   QA -0.0706
   SK -0.0322
   SA -0.1233
   SI -0.0141
   TA -0.0142
   UAE -0.0656
   AUS -0.0230
  BEL -0.0006
  CYP -0.0085
  CR  -0.0398
 DEN  -0.0423
   EST -0.0604
   FIN -0.0227
   FRA -0.0085
  GER -0.0272
  GrE -0.3519
  ICE -0.0210
  IRE -0.0057
  LAT -0.0595
 LITH -0.0451
 LUXE -0.0023
 MAL  -0.0351
 NETH -0.0048
   NOR -0.0495
   POL -0.0081
 PORT -0.0044
 SLOVA -0.1210
 SLOVE -0.0031
   SPA -0.0213
   SWE -0.0106
 SWIT -0.0152
   UK -0.0030
 HUNG -0.0086
   CAN -0.0144
 CHIL -0.0078
   USA -0.0042
 BERM -0.0035
 AUST -0.0211
 NEWZ -0.0538" ,
  header = TRUE,stringsAsFactors=FALSE)
library(cluster)
k1<-kmeans(DMs[,2],centers=2,nstart=25)
plot(DMs[,2],col=k1$cluster,pch=19,xlim=c(1,46), ylim=c(-0.12,0))
text(1:46,DMs[,2],DMs[,1],col=k1$cluster)
legend(10,1,c("cluster 1: Highly Integrated","cluster 2: Less Integrated"),
col=1:2,pch=19)




__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Clustering of datasets

2022-09-05 Thread Jim Lemon
Hi Subhamitra,
I think the fact that you are passing a vector of values rather than a
matrix is part of the problem. As you have only one value for each
country, The points plotted will be the index on the x-axis and the
value for each country on the y-axis. Passing a value for ylim= means
that you are cutting off the lowest points. Here is an example that
will give you two clusters and show the values for the centers in the
middle of the plot. Perhaps this is all you need, but I suspect there
is more work to be done.

k2<-kmeans(DMs[,2],centers=2)
plot(DMs[,2],col=k2$cluster,pch=19,xlim=c(1,46))
text(1:46,DMs[,2],DMs[,1],col=k2$cluster)
points(rep(23,2),k2$centers,pch=1:2,cex=2,col=k2$cluster)
legend(10,1,c("cluster 1: Highly Integrated","cluster 2: Less Integrated"),
col=1:2,pch=19)

Jim

On Mon, Sep 5, 2022 at 9:31 PM Subhamitra Patra
 wrote:
>
> Dear all,
>
> I am about to cluster my datasets by using K-mean clustering techniques in
> R, but getting some type of scattered results. Herewith I pasted my code
> below. Please suggest to me where I am lacking in my code. I was pasting my
> data before applying the K-mean method as follows.
>
> DMs<-read.table(text="Country DATA
>   IS -0.0092
>   BA -0.0235
>   HK -0.0239
>   JA -0.0333
>   KU -0.0022
>   OM -0.0963
>   QA -0.0706
>   SK -0.0322
>   SA -0.1233
>   SI -0.0141
>   TA -0.0142
>   UAE -0.0656
>   AUS -0.0230
>  BEL -0.0006
>  CYP -0.0085
>  CR  -0.0398
> DEN  -0.0423
>   EST -0.0604
>   FIN -0.0227
>   FRA -0.0085
>  GER -0.0272
>  GrE -0.3519
>  ICE -0.0210
>  IRE -0.0057
>  LAT -0.0595
> LITH -0.0451
> LUXE -0.0023
> MAL  -0.0351
> NETH -0.0048
>   NOR -0.0495
>   POL -0.0081
> PORT -0.0044
> SLOVA -0.1210
> SLOVE -0.0031
>   SPA -0.0213
>   SWE -0.0106
> SWIT -0.0152
>   UK -0.0030
> HUNG -0.0086
>   CAN -0.0144
> CHIL -0.0078
>   USA -0.0042
> BERM -0.0035
> AUST -0.0211
> NEWZ -0.0538" ,
>  header = TRUE,stringsAsFactors=FALSE)
> library(cluster)
> k1<-kmeans(DMs[,2],centers=2,nstart=25)
> plot(DMs[,2],col=k1$cluster,pch=19,xlim=c(1,46), ylim=c(-0.12,0))
> text(1:46,DMs[,2],DMs[,1],col=k1$cluster)
> legend(10,1,c("cluster 1: Highly Integrated","cluster 2: Less Integrated"),
> col=1:2,pch=19)
>
>
> --
> *Best Regards,*
> *Subhamitra Patra*
> *Phd. Research Scholar*
> *Department of Humanities and Social Sciences*
> *Indian Institute of Technology, Kharagpur*
> *INDIA*
>
> [image: Mailtrack]
> 
> Sender
> notified by
> Mailtrack
> 
> 09/05/22,
> 04:55:22 PM
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] How to use AUC metric in caret

2022-09-05 Thread Neha gupta
Hello everyone

I am using nested resampling in caret (5-fold outer and bootstrap inner
resampling) and by default, it shows the "Accuracy" metric. How can I use
it for the ROC/AUC metric?

My code is:

d=readARFF("apns.arff")
index <- createDataPartition(d$isKilled , p = .70,list = FALSE)
tr <- d[index, ]
ts <- d[-index, ]

boot <- trainControl(method = "boot", number=100, search="random",
classProbs = TRUE, summaryFunction = twoClassSummary)

outer_folds <- createFolds(d$isKilled, k = 5)
boot <- trainControl(method = "boot", number=10)

CV1 <- lapply(outer_folds, function(index){
  tr <- d[-index, ]
  ts <- d[index,]

  cart1 <-train(isKilled ~ ., data = tr,
method = "rpart",

tuneLength = 20,
 metric = "Accuracy",
 preProc = c("center", "scale", "nzv"),
 trControl = boot)

  postResample(predict(cart1, ts), ts$isKilled)
})
sapply(CV1, function(x) x[3]) -> CV_MAE1
CV_MAE1

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.