Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a contour plot in R

Tags:

r

contour

I've tuned a SVM with various values for cost and gamma for a dataset

> library(e1071)
> library(foreign)
> dataframe <- read.arff("/diabetes.arff")
> index <- seq_len(nrow(dataframe))
> trainindex <- sample(index, trunc(length(index)/2))
> trainset <- dataframe[trainindex, ]
> testset <- dataframe[-trainindex, ]
> tunedsvm <- tune.svm(class ~ ., data = trainset, gamma = 2^(seq(-15,3,by=2)), cost = 2^(seq(-5,15,by=2)))
> tunedsvm

Parameter tuning of ‘svm’:

- sampling method: 10-fold cross validation 

- best parameters:
        gamma cost
 0.0001220703 2048

- best performance: 0.2187029 
> head(tunedsvm$performances)
         gamma    cost    error dispersion
1 3.051758e-05 0.03125 0.351546 0.06245835
2 1.220703e-04 0.03125 0.351546 0.06245835
3 4.882812e-04 0.03125 0.351546 0.06245835
4 1.953125e-03 0.03125 0.351546 0.06245835
5 7.812500e-03 0.03125 0.351546 0.06245835
6 3.125000e-02 0.03125 0.351546 0.06245835
> nrow(tunedsvm$performances)
[1] 110

I want to create a contour plot similar to what matlab generates (below is an example)

enter image description here

I tried the plot command but the contour I can't deduce much from the plot it generates.

> plot(tunedsvm)

enter image description here

like image 839
birdy Avatar asked Jan 09 '15 16:01

birdy


People also ask

How do you plot contour?

A contour plot is a graphical technique for representing a 3-dimensional surface by plotting constant z slices, called contours, on a 2-dimensional format. That is, given a value for z, lines are drawn for connecting the (x,y) coordinates where that z value occurs.

What is contour function in R?

The ContourFunctions R package provides functions that make it easier to make contour plots. The function cf is a quick function that can take in grid data, a function, or any data, and give a contour plot showing the function or data.

How do you plot in 3D contour?

To plot 3D contour we will use countour3() to plot different types of 3D modules. Syntax: contour3(X,Y,Z): Specifies the x and y coordinates for the values in Z. contour3(Z): Creates a 3-D contour plot containing the isolines of matrix Z, where Z contains height values on the x-y plane.

What is a contour plot example?

Example Contour Plot For example, a biologist studies the effect of stream depth and canopy cover on fish biomass. A contour plot typically contains the following elements: X and Y-axes denoting values of two continuous independent variables. Colored bands representing ranges of the continuous dependent (Z) variable.


1 Answers

tune.svm returns an object of class "tune":

> class(tunedsvm)
[1] "tune"

Therefore the relevant help page is ?plot.tune. A little reading of this reveals several useful arguments for customisation:

plot(tunedsvm, 
     transform.x = log2, transform.y = log2, # log 2 scale for x and y
     transform.z = function(x) 1 - x,        # convert error rate to accuracy rate
     swapxy = TRUE,                          # put gamma on y axis
     color.palette = terrain.colors,         # define color palette for contours
     xlab = expression(log[2](cost)), ylab = expression(log[2](gamma)),
     main = "Accuracy Rate of SVM")

This code produces the following plot:

contour plot showing accuracy rate of svm over log2 gamma vs log2 cost

like image 85
Heather Turner Avatar answered Sep 20 '22 23:09

Heather Turner