I want to graph 0.2*ln(x/40)-0.02(x-40)=-0.04ln(y/100)+0.004(y-100) in RStudio. I installed 'manipulate' package and used plotFun but it didnt work. I tried using plot.function but I couldnt find a solution. please help
Not sure what you've tried since you didn't provide any code. Perhaps this helps: Solve your equation for z (or set it = 0), then give plotFun your new expression:
library(manipulate)
library(mosaic)
plotFun(0.2*log(x/40)- 0.02*(x-40) + 0.04*log(y/100) - 0.004*(y-100) ~
y+x, surface=TRUE, ylim=c(0,5), xlim=c(0,5))
If you just want for z=0, you can use contour:
xyFun <- function(x,y){
0.2*log(x/40)- 0.02*(x-40)+0.04*log(y/100) - 0.004*(y-100)
}
x <- y <- seq(0,5, by=0.1)
z <- outer(x,y,xyFun)
contour(x,y,z, levels=0)
or just
contour(x,y,z)
Related
I'm trying to add single point to plot.tune but it ends up in a wrong place.
I'm calling
plot(radial.tune2, color.palette = topo.colors)
points(radial.tune2$best.parameters, pch=20, col='red')
where radial.tune2 is tune object.
The parameters are as follows:
> radial.tune2$best.parameters
cost gamma
52 0.385 0.04125
I think this is due to the fact, that the scale bar is not taken into account (as I re-scaled the plot, the red dot appeared in different places relative to axes).
I looked into documentation, but found nothing about adding points to plots of tune object.
I found this question which seems to have similar origin, but does not address the problem properly.
Edit: here is simplified example of the same problem.
It produces different plot, but with the same shifting-to-the-right property.
library(mlbench)
library(e1071)
data(BreastCancer)
BreastCancer$Id <- NULL
BreastCancer <- na.omit(BreastCancer)
data <- as.data.frame(lapply(BreastCancer, as.numeric))
data$Class <- BreastCancer$Class
names(data) <- names(BreastCancer)
C.range <- seq(0.1, 0.7, by=0.05)
gamma.range <- seq(0.001, 0.30, by=0.05)
set.seed(1)
radial.tune <- tune(svm, train.x=data[,-10], # 10 -> Class
train.y=data[,"Class"],
kernel="radial",
ranges=list(cost=C.range, gamma=gamma.range),
tunecontrol=tune.control(sampling="bootstrap"))
plot(radial.tune, color.palette = topo.colors)
points(radial.tune$best.parameters, pch=20, col='red')
I found out that this is actually problem of filled.countour which is being called under the hood.
See this thread for more info.
let's say I have the following code:
library(boot)
samplemean <- function(x, d) {
return(mean(x[d]))
}
results_qsec <- boot(data=mtcars$qsec, statistic = samplemean, R=1000)
results_wt <- boot(data=mtcars$wt, statistic = samplemean, R=1000)
plot(results_qsec)
plot(results_wt)
for plot(results_wt) I get the following:
can I edit what is plotted? for instance, I'd like to get rid of the graph on the right, change the title from 'Histogram of t' to 'Histogram of bananas', and have the histograms for results_qsec and results_wt on the same graph.
Can this be done? I looked at the boot doc but couldn't find anything helpful.
Thanks
Tony
How about this? You could, of course, change the bin width, etc., to make it look more like the original.
wt_t <- plot(results_wt)$t
sec_t <- plot(results_qsec)$t
par(mfrow = c(1, 2))
hist(wt_t, main = "banana")
hist(sec_t, main = "apple")
thanks, looks good.
I found another way, using ggplot:
library(ggplot2)
boot_qsec <- as.data.frame(results_qsec$t)
boot_wt <- as.data.frame(results_wt$t)
ggplot() + geom_histogram(data=boot_qsec,aes(V1)) +
geom_histogram(data=boot_wt ,aes(V1))
I have a problem with the function lines.
this is what I have written so far:
model.ew<-lm(Empl~Wage)
summary(model.ew)
plot(Empl,Wage)
mean<-1:500
lw<-1:500
up<-1:500
for(i in 1:500){
mean[i]<-predict(model.ew,data.frame(Wage=i*100),interval="confidence",level=0.90)[1]
lw[i]<-predict(model.ew,data.frame(Wage=i*100),interval="confidence",level=0.90)[2]
up[i]<-predict(model.ew,data.frame(Wage=i*100),interval="confidence",level=0.90)[3]
}
plot(Wage,Empl)
lines(mean,type="l",col="red")
lines(up,type="l",col="blue")
lines(lw,type="l",col="blue")
my problem i s that no line appears on my plot and I cannot figure out why.
Can somebody help me?
You really need to read some introductory manuals for R. Go to this page, and select one that illustrates using R for linear regression: http://cran.r-project.org/other-docs.html
First we need to make some data:
set.seed(42)
Wage <- rnorm(100, 50)
Empl <- Wage + rnorm(100, 0)
Now we run your regression and plot the lines:
model.ew <- lm(Empl~Wage)
summary(model.ew)
plot(Empl~Wage) # Note. You had the axes flipped here
Your first problem was that you flipped the axes. The dependent variable (Empl) goes on the vertical axis. That is the main reason you didn't get any lines on the plot. To get the prediction lines requires no loops at all and only a single plot call using matlines():
xval <- seq(min(Wage), max(Wage), length.out=101)
conf <- predict(model.ew, data.frame(Wage=xval),
interval="confidence", level=.90)
matlines(xval, conf, col=c("red", "blue", "blue"))
That's all there is to it.
Could anyone suggest why the following example code does not work:
require(biwavelet)
t <- seq(1/24, 365, 1/24)
A <- 2
fs <- 1/24
y <- A + sin(2*pi*fs*t)
d = cbind(t,y)
wt.t1 <- wt(d)
plot(wt.t1)
It generates an error stating:
Error in image.default(x$t, yvals, t(zvals), zlim = zlims, ylim = rev(range(yvals)), :
invalid z limits
How would I fix this problem?
Additional:
In response to Gavin Simpsons answer: If I keep the data to only include one frequency but alter the time vector, the code works fine.
require(biwavelet)
A <- 2
fs <- 1/24
y <- A + sin(2*pi*fs*t)
d <- cbind(seq(1,8760), y)
wt.t1 <- wt(d)
plot(wt.t1)
You discovered a bug in the wt.R function (errant parentheses). The bug has been fixed in version 0.12 of the biwavelet package, so both versions of your code above should now work.
Thanks for spotting the error. Please don't hesitate to email the maintainer of the package (i.e., me) about bugs in the future.
I suspect this is due to the fact that you have only a single frequency here and the function isn't set up for that. I can get a plot by adding white noise to y:
require(biwavelet)
t <- seq(1/24, 365, 1/24)
A <- 2
fs <- 1/24
y <- A + sin(2*pi*fs*t)
d <- cbind(t, y + rnorm(length(y))) ## add some white noise to y
wt.t1 <- wt(d)
plot(wt.t1)
You might wish to contact the maintainers to report the issue. I got the plot to do something when I debugged it and reversed zlim so that diff(zlim) was positive, so it might be that the author of the plot() method was making an assumption that doesn't hold in all cases.
I would like to place the value for each bar in barchart (lattice) at the top of each bar. However, I cannot find any option with which I can achieve this. I can only find options for the axis.
Create a custom panel function, e.g.
library("lattice")
p <- barchart((1:10)^2~1:10, horiz=FALSE, ylim=c(0,120),
panel=function(...) {
args <- list(...)
panel.text(args$x, args$y, args$y, pos=3, offset=1)
panel.barchart(...)
})
print(p)
I would have suggested using the new directlabels package, which can be used with both lattice and ggplot (and makes life very easy for these labeling problems), but unfortunately it doesn't work with barcharts.
Since I had to do this anyway, here's a close-enough-to-figure it out code sample along the lines of what #Alex Brown suggests (scores is a 2D array of some sort, which'll get turned into a grouped vector):
barchart(scores, horizontal=FALSE, stack=FALSE,
xlab='Sample', ylab='Mean Score (max of 9)',
auto.key=list(rectangles=TRUE, points=FALSE),
panel=function(x, y, box.ratio, groups, errbars, ...) {
# We need to specify groups because it's not actually the 4th
# parameter
panel.barchart(x, y, box.ratio, groups=groups, ...)
x <- as.numeric(x)
nvals <- nlevels(groups)
groups <- as.numeric(groups)
box.width <- box.ratio / (1 + box.ratio)
for(i in unique(x)) {
ok <- x == i
width <- box.width / nvals
locs <- i + width * (groups[ok] - (nvals + 1)/2)
panel.arrows(locs, y[ok] + 0.5, scores.ses[,i], ...)
}
} )
I haven't tested this, but the important bits (the parts determining the locs etc. within the panel function) do work. That's the hard part to figure out. In my case, I was actually using panel.arrows to make errorbars (the horror!). But scores.ses is meant to be an array of the same dimension as scores.
I'll try to clean this up later - but if someone else wants to, I'm happy for it!
If you are using the groups parameter you will find the labels in #rcs's code all land on top of each other. This can be fixed by extending panel.text to work like panel.barchart, which is easy enough if you know R.
I can't post the code of the fix here for licencing reasons, sorry.