[R] Question regarding k-fold cross validation
Dear friends, Hope you are doing great. I need to perform a k-fold cross validation analysis on the famous iris dataset, with k = 5. Now, the model I am using is a multinomial logit, because the dependent variable is species and it has three classes (setosa, versicolor and virginica). The packages I am using are caret and nnet. Below is my R code: #specifying the cross-validation method ctrl <- trainControl(method = "cv", number = 5) model = train(multinom(as.factor(iris_frame$species) ~ iris_frame$sepal_length + iris_frame$sepal_width + iris_frame$petal_length + iris_frame$petal_width), trControl = ctrl) Now, R throws the following message: # weights: 18 (10 variable) initial value 164.791843 iter 10 value 16.119063 iter 20 value 7.103299 iter 30 value 6.191105 iter 40 value 5.984259 iter 50 value 5.961820 iter 60 value 5.955196 iter 70 value 5.952041 iter 80 value 5.950570 iter 90 value 5.950178 iter 100 value 5.950059 final value 5.950059 stopped after 100 iterations Error in x[0, , drop = FALSE] : incorrect number of dimensions What am I doing wrong? How could I accomplish the 5-fold cross validation on the iris data set? I am providing the dput for the iris data set that I am using below: dput(iris) structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4, 4.6, 5, 4.4, 4.9, 5.4, 4.8, 4.8, 4.3, 5.8, 5.7, 5.4, 5.1, 5.7, 5.1, 5.4, 5.1, 4.6, 5.1, 4.8, 5, 5, 5.2, 5.2, 4.7, 4.8, 5.4, 5.2, 5.5, 4.9, 5, 5.5, 4.9, 4.4, 5.1, 5, 4.5, 4.4, 5, 5.1, 4.8, 5.1, 4.6, 5.3, 5, 7, 6.4, 6.9, 5.5, 6.5, 5.7, 6.3, 4.9, 6.6, 5.2, 5, 5.9, 6, 6.1, 5.6, 6.7, 5.6, 5.8, 6.2, 5.6, 5.9, 6.1, 6.3, 6.1, 6.4, 6.6, 6.8, 6.7, 6, 5.7, 5.5, 5.5, 5.8, 6, 5.4, 6, 6.7, 6.3, 5.6, 5.5, 5.5, 6.1, 5.8, 5, 5.6, 5.7, 5.7, 6.2, 5.1, 5.7, 6.3, 5.8, 7.1, 6.3, 6.5, 7.6, 4.9, 7.3, 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.2, 1.6, 1.5, 1.6, 1.5, 1.3, 1.3, 1.3, 1.2, 1.4, 1.2, 1, 1.3, 1.2, 1.3, 1.3, 1.1, 1.3, 2.5, 1.9, 2.1, 1.8, 2.2, 2.1, 1.7, 1.8, 1.8, 2.5, 2, 1.9, 2.1, 2, 2.4, 2.3, 1.8, 2.2, 2.3, 1.5, 2.3, 2, 2, 1.8, 2.1, 1.8, 1.8, 1.8, 2.1, 1.6, 1.9, 2, 2.2, 1.5, 1.4, 2.3, 2.4, 1.8, 1.8, 2.1, 2.4, 2.3, 1.9, 2.3, 2.5, 2.3, 1.9, 2, 2.3, 1.8), Species = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("setosa", "versicolor", "virginica"), class = "factor")), class = "data.frame", row.names = c(NA, -150L)) Any help and/or guidance will be greatly appreciated. Best
Re: [R] Sorted index of values in matrix
Thankyou very much. It worked. Eliza From: Bert Gunter Sent: Friday 15 April 2022 17:00 To: Jeff Newmiller Cc: R-help ; Eliza Botto Subject: Re: [R] Sorted index of values in matrix ... But Ivan's solution -- which I had to think about -- is better, and it also generalizes to arbitrary arrays: arrayInd(order(qq), .dim = dim(qq)) Bert Gunter On Fri, Apr 15, 2022 at 9:25 AM Bert Gunter wrote: > > ... and here is another version of Jeff's solution using the built in > row() and col() functions instead of explicitly creating the indices > (but his version generalizes to arbitrary arrays; this does not): > > qq <- matrix(12:7, nrow =3) ## start with a non-sorted matrix > > qq > [,1] [,2] > [1,] 129 > [2,] 118 > [3,] 107 > > sort(qq) > [1] 7 8 9 10 11 12 ## the sorted values > > > o <- order(qq) > > cbind(row(qq)[o], col(qq)[o]) > ## matrix of (row, col) indices of sorted values in original unsorted matrix > [,1] [,2] > [1,]32 > [2,]22 > [3,]12 > [4,]31 > [5,]21 > [6,]11 > > Cheers, > Bert Gunter > > On Fri, Apr 15, 2022 at 8:33 AM Jeff Newmiller > wrote: > > > > Please stop posting in HTML format. > > > > I think your specified result coordinates are in (col,row) order, which is > > not normal for R. The solution below yields (row,col) order. > > > > qq <- structure(1:6, .Dim = 3:2, .Dimnames = list(c("1", "2", "3"), c("1", > > "2"))) > > pos <- expand.grid( r = seq.int( nrow( qq ) ) > > , c = seq.int( ncol( qq ) ) ) > > qq #'confirm source layout is column major > > pos[ order( qq ), ] # (row,col) > > pos[ order( qq ), 2:1 ] # (col,row) > > > > > > On April 15, 2022 7:53:36 AM PDT, Eliza Botto > > wrote: > > >deaR useRs, > > > > > >I have this following simple dataset > > > > > >> dput(qq) > > > > > >structure(1:6, .Dim = 3:2, .Dimnames = list(c("1", "2", "3"), > > >c("1", "2"))) > > > > > >I want to extract row and column index of the sorted values of this matrix > > > > > >For example, > > > > > >sort(qq) command will execute the sorted values of this matrix as (1 2 3 4 > > >5 6). Instead of values, I want the row and column names of these sorted > > >values as (1,1), (1,2), (1,3), (2,1), (2,2), (2,3) > > > > > >I thankyou very much in advance for your help. > > > > > >Eliza > > > > > > > > > [[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. > > > > -- > > Sent from my phone. Please excuse my brevity. > > > > __ > > 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. [[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.
Re: [R] Need help with netmeta package
While you *may* get a useful reply here, there is an r-sig-meta-analysis list to which this would be better posted: https://stat.ethz.ch/mailman/listinfo/r-sig-meta-analysis Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Fri, Apr 15, 2022 at 1:04 PM Roopa Subbaiah wrote: > > Hi all, > > We are conducting network metaanalysis of 3 treatments compared to placebo > using netmeta analysis. We have mean sd values for an outcome. How do we > calculate z scores using netmeta package? Thank you > > [[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] Need help with netmeta package
Hi all, We are conducting network metaanalysis of 3 treatments compared to placebo using netmeta analysis. We have mean sd values for an outcome. How do we calculate z scores using netmeta package? Thank you [[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.
Re: [R] Sorted index of values in matrix
... and here is another version of Jeff's solution using the built in row() and col() functions instead of explicitly creating the indices (but his version generalizes to arbitrary arrays; this does not): qq <- matrix(12:7, nrow =3) ## start with a non-sorted matrix > qq [,1] [,2] [1,] 129 [2,] 118 [3,] 107 > sort(qq) [1] 7 8 9 10 11 12 ## the sorted values > o <- order(qq) > cbind(row(qq)[o], col(qq)[o]) ## matrix of (row, col) indices of sorted values in original unsorted matrix [,1] [,2] [1,]32 [2,]22 [3,]12 [4,]31 [5,]21 [6,]11 Cheers, Bert Gunter On Fri, Apr 15, 2022 at 8:33 AM Jeff Newmiller wrote: > > Please stop posting in HTML format. > > I think your specified result coordinates are in (col,row) order, which is > not normal for R. The solution below yields (row,col) order. > > qq <- structure(1:6, .Dim = 3:2, .Dimnames = list(c("1", "2", "3"), c("1", > "2"))) > pos <- expand.grid( r = seq.int( nrow( qq ) ) > , c = seq.int( ncol( qq ) ) ) > qq #'confirm source layout is column major > pos[ order( qq ), ] # (row,col) > pos[ order( qq ), 2:1 ] # (col,row) > > > On April 15, 2022 7:53:36 AM PDT, Eliza Botto wrote: > >deaR useRs, > > > >I have this following simple dataset > > > >> dput(qq) > > > >structure(1:6, .Dim = 3:2, .Dimnames = list(c("1", "2", "3"), > >c("1", "2"))) > > > >I want to extract row and column index of the sorted values of this matrix > > > >For example, > > > >sort(qq) command will execute the sorted values of this matrix as (1 2 3 4 5 > >6). Instead of values, I want the row and column names of these sorted > >values as (1,1), (1,2), (1,3), (2,1), (2,2), (2,3) > > > >I thankyou very much in advance for your help. > > > >Eliza > > > > > > [[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. > > -- > Sent from my phone. Please excuse my brevity. > > __ > 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.
Re: [R] Sorted index of values in matrix
В Fri, 15 Apr 2022 14:53:36 + Eliza Botto пишет: > sort(qq) command will execute the sorted values of this matrix as (1 > 2 3 4 5 6). Instead of values, I want the row and column names of > these sorted values as (1,1), (1,2), (1,3), (2,1), (2,2), (2,3) Combine order() to get the sorting permutation with arrayInd() to get row and column indices from absolute indices with colnames() and rownames() to get the names themselves. -- Best regards, Ivan __ 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] Sorted index of values in matrix
deaR useRs, I have this following simple dataset > dput(qq) structure(1:6, .Dim = 3:2, .Dimnames = list(c("1", "2", "3"), c("1", "2"))) I want to extract row and column index of the sorted values of this matrix For example, sort(qq) command will execute the sorted values of this matrix as (1 2 3 4 5 6). Instead of values, I want the row and column names of these sorted values as (1,1), (1,2), (1,3), (2,1), (2,2), (2,3) I thankyou very much in advance for your help. Eliza [[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.