2

I have the following data in a csv file.

y,x1,x2,x3,x4,x5,x6,x7,x8,x9
10,2113,1985,38.9,64.7,4,868,59.7,2205,1917
11,2003,2855,38.8,61.3,3,615,55,2096,1575
11,2957,1737,40.1,60,14,914,65.6,1847,2175
13,2285,2905,41.6,45.3,-4,957,61.4,1903,2476
10,2971,1666,39.2,53.8,15,836,66.1,1457,1866
11,2309,2927,39.7,74.1,8,786,61,1848,2339
10,2528,2341,38.1,65.4,12,754,66.1,1564,2092
11,2147,2737,37,78.3,-1,761,58,1821,1909
4,1689,1414,42.1,47.6,-3,714,57,2577,2001
2,2566,1838,42.3,54.2,-1,797,58.9,2476,2254
7,2363,1480,37.3,48,19,984,67.5,1984,2217
example = data.frame(x1,x2,x3,x4,y)

How can I graph the variables x1, x2, x3 using scatter3D(x,y,z)?

I have tried:

library("plot3D")
with(example,scatter3D(y ~ x1 + x2 + x3))

But I get error:

Error in min(x,na.rm) : invalid 'type' (list) of argument

Werner Hertzog
  • 2,002
  • 3
  • 24
  • 36
royer
  • 615
  • 1
  • 6
  • 19
  • What have you tried and what error message did you get? Have you read the manual page for the function or the vignette for the package? – dcarlson Nov 16 '19 at 22:44
  • yes, but obtain error invalid type of argument – royer Nov 16 '19 at 22:57
  • did you want a regression plane or surface from these 3D data? – Ben Nov 16 '19 at 23:12
  • I could try to convert all the data x1, x2, x3 **into an array** `xx = as.array(cbind(x1,x2,x3))`. The graph that I want the: **plot points**, but **"z"** what value does it take? – royer Nov 16 '19 at 23:31
  • 1
    It is 3d not 4d. Try `with(example, scatter3D(y, x1, x2))`. – dcarlson Nov 16 '19 at 23:36
  • If you have 3 series of points the only option would be to predict one of them in terms of the other two. Plotting y on the basis of x1,x2,x3 would take 4 dimensions. You could conceivably plot y on the basis of two of those holding the third one constant. ( deleted my earlier comment about plot3D not plotting regression fits. It can but the fit argument is given to the `surf` argument.) – IRTFM Nov 16 '19 at 23:39
  • if it works, rather if I have many values such as **x1, x2, y, x3, x4** and I want to graph them. Could this library be used? – royer Nov 16 '19 at 23:41
  • Your last question is not clear regarding what is meant by "graph them". What is clear is that no function would be able to make a 5 dimensional plot. – IRTFM Nov 16 '19 at 23:43
  • ok I understand, I was asking to create multiple graphs with the variables and of course I also want to know how good they would be adding style. – royer Nov 16 '19 at 23:52

1 Answers1

2

Looks like you want to plot a regression plane. The scatter3d function in package car will do that. You need to install car and rgl. First let's make your data more accessible:

dput(example)
structure(list(y = c(10L, 11L, 11L, 13L, 10L, 11L, 10L, 11L, 
4L, 2L, 7L), x1 = c(2113L, 2003L, 2957L, 2285L, 2971L, 2309L, 
2528L, 2147L, 1689L, 2566L, 2363L), x2 = c(1985L, 2855L, 1737L, 
2905L, 1666L, 2927L, 2341L, 2737L, 1414L, 1838L, 1480L), x3 = c(38.9, 
38.8, 40.1, 41.6, 39.2, 39.7, 38.1, 37, 42.1, 42.3, 37.3), x4 = c(64.7, 
61.3, 60, 45.3, 53.8, 74.1, 65.4, 78.3, 47.6, 54.2, 48), x5 = c(4L, 
3L, 14L, -4L, 15L, 8L, 12L, -1L, -3L, -1L, 19L), x6 = c(868L, 
615L, 914L, 957L, 836L, 786L, 754L, 761L, 714L, 797L, 984L), 
    x7 = c(59.7, 55, 65.6, 61.4, 66.1, 61, 66.1, 58, 57, 58.9, 
    67.5), x8 = c(2205L, 2096L, 1847L, 1903L, 1457L, 1848L, 1564L, 
    1821L, 2577L, 2476L, 1984L), x9 = c(1917L, 1575L, 2175L, 
    2476L, 1866L, 2339L, 2092L, 1909L, 2001L, 2254L, 2217L)),
    class = "data.frame", row.names = c(NA, -11L))

install.packages("car")
install.packages("rgl")
library(car)
library(rgl)
scatter3d(y~x1+x2, example)

The plot window will be small. Use the mouse to drag the lower right corner to make it bigger. You can drag within the plot to rotate it.

3d plot

dcarlson
  • 10,936
  • 2
  • 15
  • 18
  • Upvoted for the excellence of the answer and the coolness of the 3D plot. – James Phillips Nov 17 '19 at 01:33
  • Can I use this library? **`library("plot3D")`**,or it's necessary to use these :`library(car) library(rgl)` – royer Nov 17 '19 at 03:25
  • Read the [vignette](https://cran.r-project.org/web/packages/plot3D/vignettes/plot3D.pdf) for **package** (not library) `plot3D`. It seems to have many functions, but I didn't see examples plotting a regression plane. You haven't told us anything about what you are trying to do except that you want to do 3d plots. Earlier I suggested `with(example, scatter3D(y, x1, x2))`. Did that not work? If not, why? – dcarlson Nov 17 '19 at 03:46
  • excuse me, it's **`library("plot3Drgl")`**.My goal was to try to create an elegant graphic but have the function **add = TRUE** – royer Nov 18 '19 at 13:29