[英]How to plot a multivariate function with known coefficients using -scatter3d-
I am trying to use R to plot a function that takes the form: 我正在尝试使用R来绘制采用以下形式的函数:
$$z=\\beta_{0}+\\beta_{1}x+\\beta_{2}x^{2}+\\beta_{3}y+\\beta_{4}y^{2}+\\beta_{5}x*y$$ $$ z = \\ beta_ {0} + \\ beta_ {1} x + \\ beta_ {2} x ^ {2} + \\ beta_ {3} y + \\ beta_ {4} y ^ {2} + \\ beta_ {5} x * y $$
Where I have numerical values for all the $\\beta$'s. 我在其中具有所有$ \\ beta $的数值。 I can't seem to find an example of using the the functional form of -scatter3d- and I am not running a regression within R nor plotting the series of z, x, and y. 我似乎找不到使用-scatter3d-的函数形式的示例,并且我没有在R中运行回归,也没有绘制z,x和y的序列。
I currently have 我目前有
scatter3D(df$z=0.0279x-0.0000188x_sq+0.0422y-0.00708y_sq-0.000181x_y, df)
And haven't even added anything else to it because I get an error message: 甚至没有添加任何其他内容,因为我收到一条错误消息:
Error: unexpected '=' in "scatter3D(df$z="
So I am assuming it is a syntax problem but can't seem to find anything about it. 因此,我假设这是一个语法问题,但似乎找不到任何相关信息。
A couple of things: 有两件事:
You won't be able to plot 4 continuous variables against a single dependent on only 2 axes. 您将无法针对仅取决于2个轴的单个变量绘制4个连续变量。
Formulas follow the pattern y ~ x + z
so change your =
to a ~
. 公式遵循y ~ x + z
的模式,因此将=
更改为~
。
You don't need a df$
in front of any variable since you specify the data with the ..., df
part. 由于您使用..., df
部分指定了数据,因此不需要在任何变量前使用df$
。
Formula's don't accept coefficients but you can easily change them in the original data frame like this: 公式不接受系数,但是您可以像这样在原始数据框中轻松更改它们:
df<-data.frame(x=rnorm(100,5,1),
y=rnorm(100,50,1),
z=rnorm(100,500,1))
df$x_sq<-0.000188*(df$x)^2
df$y_sq<-0.0708*(df$y)^2
df$y2<-0.0422*(df$y)
df$x2<-0.0279*(df$x)
scatter3d(z~x2+y2,df)
Also https://meta.stackexchange.com/questions/30559/latex-on-stack-overflow 也是https://meta.stackexchange.com/questions/30559/latex-on-stack-overflow
If you want a surface, not a scatter, then modify coefficients in this: 如果您想要一个表面而不是一个散点,则可以在其中修改系数:
x <- seq(-10, 10, length.out = 50)
y <- x
zf<-function(x,y){1*x^2+1*x-0.1*y^2-0.1*y+0.1*y*1*x}
z <- outer(x, y, zf)
surface3d(x,y,z)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.