编织rmarkDown引发了Contour()的错误打印结果

发布于 2025-01-31 08:06:48 字数 1347 浏览 1 评论 0原文

我想将以下代码编织到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.

contour plot

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文