编织rmarkDown引发了Contour()的错误打印结果
我想将以下代码编织到HTML文档中。该代码在rstudio中运行良好,但是当我尝试编织时失败 - 我在这里缺少什么?
library(e1071)
load(url("http://www.stanford.edu/~hastie/ElemStatLearn/datasets/ESL.mixture.rda"))
names(ESL.mixture)
attach(ESL.mixture)
plot(x,col=y+2,pch=c(17,19)[y+1],xlab=expression(x[1]),ylab=expression(x[2]),cex=1.5,cex.axis=1.5,cex.lab=1.5)
data <- data.frame(x, as.factor(y) )
colnames(data) <- c("x1", "x2", "y1")
attach(data)
svmfit <- svm(y1 ~ x1 + x2, data = data, kernel = "radial", cost = 32.1, gamma = 0.5, scale = FALSE)
xgrid <- expand.grid(x1=px1,x2= px2)
ygrid <- predict(svmfit,xgrid)
plot(x,col=y+2,pch=c(17,19)[y+1],xlab=expression(x[1]),ylab=expression(x[2]),cex=1.5,cex.axis=1.5,cex.lab=1.5)
points(xgrid,col=as.numeric(ygrid)+1,pch=20,cex=.2)
contour(px1,px2,matrix(ygrid,69,99),level=1,add=TRUE)
这会根据需要产生以下图。
尝试将其编织到HTML报告中时,会引发以下错误。数据的尺寸绝对是正确的,并且在Contour()调用中指定的尺寸也看起来还不错,因此尚不清楚如何解决此问题。
Error in contour.default(px1, px2, matrix(ygrid, 69, 99), level = 1, add = TRUE) :
dimension mismatch
Calls: <Anonymous> ... eval_with_user_handlers -> eval -> eval -> contour -> contour.default
I want to knit the following code into an html document. The code runs fine in Rstudio but fails when I attempt to knit - what am I missing here?
library(e1071)
load(url("http://www.stanford.edu/~hastie/ElemStatLearn/datasets/ESL.mixture.rda"))
names(ESL.mixture)
attach(ESL.mixture)
plot(x,col=y+2,pch=c(17,19)[y+1],xlab=expression(x[1]),ylab=expression(x[2]),cex=1.5,cex.axis=1.5,cex.lab=1.5)
data <- data.frame(x, as.factor(y) )
colnames(data) <- c("x1", "x2", "y1")
attach(data)
svmfit <- svm(y1 ~ x1 + x2, data = data, kernel = "radial", cost = 32.1, gamma = 0.5, scale = FALSE)
xgrid <- expand.grid(x1=px1,x2= px2)
ygrid <- predict(svmfit,xgrid)
plot(x,col=y+2,pch=c(17,19)[y+1],xlab=expression(x[1]),ylab=expression(x[2]),cex=1.5,cex.axis=1.5,cex.lab=1.5)
points(xgrid,col=as.numeric(ygrid)+1,pch=20,cex=.2)
contour(px1,px2,matrix(ygrid,69,99),level=1,add=TRUE)
This produced the following plot as desired.
When attempting to knit it into a html report, it throws the following error. The dimensions of the data are definitely correct and those specified in the contour() call look fine as well, so am unclear on how to solve this.
Error in contour.default(px1, px2, matrix(ygrid, 69, 99), level = 1, add = TRUE) :
dimension mismatch
Calls: <Anonymous> ... eval_with_user_handlers -> eval -> eval -> contour -> contour.default
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论