Hello, I am trying to get the 'rank' function to work for me, but not sure what I am doing wrong. Please help. I ran the following commands: data = read.table("test1.csv", head=T, as.is=T, na.string=".", row.nam=NULL) X1 = as.factor(data[[3]]) X2 = as.factor(data[[4]]) X3 = as.factor(data[[5]]) Y = data[[2]] model = lm(Y ~ X1*X2*X3, na.action = na.exclude) fmodel = fitted(model) fmodel (First line is shown below.....) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 180.3763 180.3763 180.3763 180.3763 180.4546 180.3763 177.9245 177.9245 181.3859 180.3763 NA 180.4546 180.3763 180.4546 180.3763 180.3763 180.4546 Then I run: fmodel.rank = rank(fmodel) fmodel.rank (First line is shown below....) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 375.0 222.0 68.5 68.5 402.5 222.0 33.5 33.5 465.5 222.0 500.0 402.5 222.0 402.5 222.0 222.0 378.5 222.0 222.0 222.0 222.0 222.0 402.5 222.0 33.5 222.0 As you can see, first 4 values of 'fmodel' are 180.3763, so after running rank(fmodel) I expected the ranks of first 4 to be the same, but they are not. What am I doing wrong? Please let me know. Thanks. [[alternative HTML version deleted]]
Hi, I believe that the reason is that even though the first 4 elements of your fmodel look equal (when rounded to 4 decimal places) they are actually not. To check this try fmodel[1:4]-fmodel[1] --- On Thu, 11/2/10, Something Something <mailinglists19 at gmail.com> wrote:> From: Something Something <mailinglists19 at gmail.com> > Subject: [R] Question about rank() function > To: r-help at r-project.org > Received: Thursday, 11 February, 2010, 6:23 PM > Hello, > > I am trying to get the 'rank' function to work for me, but > not sure what I > am doing wrong.? Please help. > > I ran the following commands: > > data = read.table("test1.csv", head=T, as.is=T, > na.string=".", row.nam=NULL) > X1 = as.factor(data[[3]]) > X2 = as.factor(data[[4]]) > X3 = as.factor(data[[5]]) > Y = data[[2]] > model = lm(Y ~ X1*X2*X3, na.action = na.exclude) > fmodel = fitted(model) > fmodel > (First line is shown below.....) > > > ? ? ???1? ? ? > ? 2? ? ? ? 3? ? ? > ? 4? ? ? ? 5? ? ? > ? 6? ? ? ? 7? ? ? > ? 8 > ???9? ? ???10? > ? ???11? ? > ???12? ? ???13? > ? ???14? ? > ???15? ? ???16 > 17 > 180.3763 180.3763 180.3763 180.3763 180.4546 180.3763 > 177.9245 177.9245 > 181.3859 180.3763? ? ???NA > 180.4546 180.3763 180.4546 180.3763 180.3763 > 180.4546 > > Then I run: > fmodel.rank = rank(fmodel) > fmodel.rank > (First line is shown below....) > > ? ? 1? ???2? > ???3? ???4? > ???5? ???6? > ???7? ???8? > ???9? ? 10? ? 11? > ? 12 > 13? ? 14? ? 15? ? 16? > ? 17? ? 18? ? 19? ? > 20? ? 21? ? 22? ? 23? > ? 24? ? 25 > ???26 > 375.0 222.0? 68.5? 68.5 402.5 222.0? > 33.5? 33.5 465.5 222.0 500.0 402.5 > 222.0 402.5 222.0 222.0 378.5 222.0 222.0 222.0 222.0 222.0 > 402.5 222.0 > 33.5 222.0 > > > As you can see, first 4 values of 'fmodel' are 180.3763, so > after running > rank(fmodel) I expected the ranks of first 4 to be the > same, but they are > not.? What am I doing wrong?? Please let me > know.? Thanks. > > ??? [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org > mailing list > 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. >
To follow up on Moshe's post, do rank(fmodel) rank(round(fmodel,4)) rank(fmodel)==rank(round(fmodel,4)) If the two are not identical, you have the explanation Moshe suggested, just that this approach is somewhat more comprehensive as it works on the entire fmodel vector. See the example below: x=c(1.0001,1.0002,1.0003,1.0004,2.0001,2.0002,2.0003,3.0001,3.0002,4) rank(x) rank(round(x,2)) rank(x)==rank(round(x,2)) HTH, Daniel ------------------------- cuncta stricte discussurus ------------------------- -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Something Something Sent: Thursday, February 11, 2010 2:23 AM To: r-help at r-project.org Subject: [R] Question about rank() function Hello, I am trying to get the 'rank' function to work for me, but not sure what I am doing wrong. Please help. I ran the following commands: data = read.table("test1.csv", head=T, as.is=T, na.string=".", row.nam=NULL) X1 = as.factor(data[[3]]) X2 = as.factor(data[[4]]) X3 = as.factor(data[[5]]) Y = data[[2]] model = lm(Y ~ X1*X2*X3, na.action = na.exclude) fmodel = fitted(model) fmodel (First line is shown below.....) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 180.3763 180.3763 180.3763 180.3763 180.4546 180.3763 177.9245 177.9245 181.3859 180.3763 NA 180.4546 180.3763 180.4546 180.3763 180.3763 180.4546 Then I run: fmodel.rank = rank(fmodel) fmodel.rank (First line is shown below....) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 375.0 222.0 68.5 68.5 402.5 222.0 33.5 33.5 465.5 222.0 500.0 402.5 222.0 402.5 222.0 222.0 378.5 222.0 222.0 222.0 222.0 222.0 402.5 222.0 33.5 222.0 As you can see, first 4 values of 'fmodel' are 180.3763, so after running rank(fmodel) I expected the ranks of first 4 to be the same, but they are not. What am I doing wrong? Please let me know. Thanks. [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list 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.