randu dataset, part 2

November 19, 2011
By

[This article was first published on R snippets, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In my last post I have plotted randu dataset to show that all its points lie on 15 parallel planes. But I was not fully satified with the solution and decided to show this numerically.

It can be done in four steps:

  1. identifying four points lying on the same plane and finding its equation (we know that we have 15 planes so it is enough to check 15*3+1 points to find them);
  2. applying the equation to all points in randu dataset to divide them into 15 classes;
  3. veryfying that the classes separate points into 15 parallel planes;
  4. plotting the solution changing colors for points on different planes.

Here is the code I used to do this:

library(rgl)
library(caTools)
# STEP 1
all.combs <- combs(1:46, 4)
i <- 1
repeat {
      model <- lm(z ~ x + y, data = randu[all.combs[i, ], ])
      if (summary(model)$r.squared > 0.99999) {
            break
      }
      i <- i + 1
}
# STEP 2
line.class <- predict(model, randu) randu$z
line.class <- factor(round(line.class) + 10)
# STEP 3
summary(lm(z ~ x + y + line.class, data = randu))
# STEP 4
with(randu, plot3d(x, y, z, axes = FALSE, col = line.class,
       xlab = “”, ylab = “”, zlab = “”))
rgl.viewpoint(theta = 3.8, phi = 3.8, fov = 0, zoom = 0.7)

At step three we can see that the model obtains perfect fit. The final figure is plotted below:

To leave a comment for the author, please follow the link and comment on their blog: R snippets.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)