How to change line width when plotting a locfit object in R? - r

This sounds like a really basic question and it probably is but I can't figure out how to change the line width when plotting a locfit object. If you do a simple test, such as:
plot(locfit::locfit(~rnorm(n = 1000)))
and compare it with
plot(locfit::locfit(~rnorm(n = 1000)), lwd = 2.0)
You will see that the plotted line has the same thickness in both plots. So using lwd does not work when plotting a locfit object? Is there any workaround?
Thanks!

You could use your model to predict the output to use lines on an empty plot which makes it possible to change the linewidth with lwd like this:
library(locfit)
#> locfit 1.5-9.7 2023-01-02
set.seed(7)
fit <- locfit(~rnorm(n = 1000))
plot(fit)
set.seed(7)
xvalues <- seq(min(rnorm(n = 1000)), max(rnorm(n = 1000)), length.out = 100)
pred <- predict(fit, xvalues)
plot(1, type="n", xlab="", ylab="", xlim=c(-3, 3), ylim=c(0, 0.4))
lines(xvalues, pred, lwd = 10)
Created on 2023-02-09 with reprex v2.0.2

There is not currently a way to do that in the existing function. When you call plot() on the locfit object, it calls preplot.locift() on your object, and then plot.preplot.locfit() which calls plot.locfit.1d(). The relevant lines from the code are:
plot(xev[ord], yy[ord], type = "n", xlab = xlab, ylab = ylab,
main = main, xlim = range(x$xev[[1]]), ylim = ylim,
...)
}
lines(xev[ord], yy[ord], type = type, lty = lty, col = col)
As you can see, the ... goes through to the plot function, but the line actually gets added with lines() which does not have access to other arguments specified in ...

Related

How do I use the parameter 'bw=sj' in R

We have been told to make a histogram and line using our given data. I can make the histogram I think correctly. However we were told to use bw='sj' in our density function. I do not understand how I would put this to use.
i tried putting it in the hist() function as I thought it is a parameter however I get an error that says:
Warning messages:
1: In plot.window(xlim, ylim, "", ...) : "bw" is not a graphical parameter
2: In title(main = main, sub = sub, xlab = xlab, ylab = ylab, ...) :
"bw" is not a graphical parameter
3: In axis(1, ...) : "bw" is not a graphical parameter
4: In axis(2, ...) : "bw" is not a graphical parameter
This is part of my code that deals with the problem in R.
# histogram 1
rdi4p -> data_shhs[,'rdi4p']
hist(rdi4p ,probability=TRUE,col=rgb(0,0,1,1/4),breaks=30,
xlab="rdi4p",
main="Histogram 1",col.axis="blue")
lines(x=density(x= rdi4p),type="l",col="blue",lwd=3)
Of course, I don't have your data to work on (in particular we would need to know what rdi4p and sj were to make this fully reproducible), so I'll make up our own values for these variables:
set.seed(1) # Make example reproducible
rdi4p <- rnorm(1000) # Vector of 1000 samples from normal distribution
sj <- diff(range(rdi4p))/30 # 1/30 of the range of vector rdi4p
Now we draw the histogram using your code:
hist(rdi4p, probability = TRUE, col = rgb(0, 0, 1, 1/4), breaks = 30,
xlab = "rdi4p", main = "Histogram 1", col.axis = "blue")
and then we add the line. Note that we have to pass the parameter bw = sj to the density function, which is itself sitting inside the call to lines:
lines(x = density(x = rdi4p, bw = sj), type = "l", col = "blue", lwd = 3)

How to create a ROC curve plot with log scale for X axis?

I have a big database and I run a ROC curve with nsROC package. I need to have log scale on x-axis. How can I do it with plot function?
ns_roc <- gROC(sei$score, sei$label)
par(xlog = TRUE)
plot(ns_roc, xlim = range(1e-6:1), ylim = range(0:1), xaxs = "r", yaxs = "r")
This is my error message:
Error in plot.default(xx, c(0, obj$roc, 1), type = type, xlim = c(0, 1), :
formal argument "xlim" matched by multiple actual arguments
The issue is that plot here is not the generic R plot function. It is invoking plot.groc which is a separate function that defines xlim explicitly.
Type plot.groc after you install your package to see the whole function. You will see this inside the function:
[removed]
plot(xx, c(0, obj$roc, 1), type = type, xlim = c(0, 1), ylim = c(0,
1), lwd = lwd, xlab = xlab, ylab = ylab, main = main,
...)
axis(1, at = seq(0, 1, 0.01), labels = F, tck = -0.01)
axis(1, at = seq(0.1, 0.9, 0.1), labels = F, tck = -0.02)
[removed]
This is your problem.
You may have to make your own version of this function.
It is not clear to me whether you want logarithmic ticks on your x-axis (e.g., axis(1, c(0,0.001,0.01,0.1,1)) instead of equally-spaced tickmarks) or if you want the whole x-axis on the log scale (e.g., log(False-Positive Rate) between -Inf and 0 at different powers of 10).
I'm not sure why using a logarithmic axis (-Inf to 0) is a good idea here, which is what you seem to want to graph. You may not be able to use the nsROC package without 1) modifying your ROC curve data at x=0, and 2) writing your own functions for a log-axis in an otherwise perfectly interpretable set of ROC curves. It makes interpreting the AUC more difficult as well.

R, how to use pch with time series plot

I would like to plot a time series (meaning line graph with x axis as time) and specify a plotting character to use. None of the following has worked
a1 = as.xts(ts(c(5,3,7,2,4,8,3), start=c(1980,1), freq=4))
library('lattice')
xyplot(a1, col="red", pch=2)
xyplot(a1, col="red", par.settings = list(superpose.symbol = list(col = 1, pch = 2)),)
ts.plot(ts(a1), pch=2)
plot(a1, phc=2)
I would prefer a lattice solution, but would accept any solution if lattice can not do this.
By default, time series plots in R use type = "l", which means that you get a line but no point characters. To get both, you can change your type to "b".
xyplot(a1, col = "red", pch = 2, type = "b")
This yields:
The same logic applies to the base plot function; simply add type = "b".

Plot line of a function in R

I have the following script:
FGM = function (n,r,z){
x = r*sqrt(n)/(2*z)
Px = 1-pnorm(x)
}
re = 10000
data = data.frame(abs(rnorm(re,0,1)), abs(rnorm(re,0,1)), abs(rnorm(re,0,1)))
colnames(data) = c("n","r","z")
data$Px = FGM(data$n,data$r,data$z)
data$x = data$r*sqrt(data$n)/(2*data$z)
par(mar=c(4.5,4.5,1,1))
plot(data$x,data$Px, xlim = c(0,3), pch = 19, cex = 0.1, xaxs="i", yaxs="i",
xlab = expression(paste("Standardized mutational size (",italic(x), ")")),
ylab = expression(paste("P"[a],"(",italic(x),")")))
Which is a recreation of the graph found here (box 2). You can see in this script that I do this by just plotting 10000 small black points with various values of n,z, and r. This seems like an ugly work around, I think I should just be able to give R my function
FGM = function (n,r,z){
x = r*sqrt(n)/(2*z)
Px = 1-pnorm(x)
}
and have it plot a line on a graph. However, a few hours of scouring the web has been unproductive, and I tried a few ways with abline and lines but nothing worked, is there a way of doing it with these functions or another function?
Tried this...
plot(data$x,data$Px, xlim = c(0,3), ylim = c(0,0.5), xaxs="i", yaxs="i",
xlab = expression(paste("Standardized mutational size (",italic(x), ")")),
ylab = expression(paste("P"[a],"(",italic(x),")")), type = "n")
curve(1-pnorm(r*sqrt(n)/(2*z)), add=T)
>Error in curve(1 - pnorm(r * sqrt(n)/(2 * z)), add = T) :
'expr' must be a function, or a call or an expression containing 'x'
>
#PaulRegular offered this solution but it still plots based on data, not the formula itself. I'm looking for a solution which can produce the curve properly without large values of "re" - using the following but with "re" set to 10 you can see what I mean...
data <- data[order(data$x),]
lines(data$x, data$Px, lwd=1)
You can pass a function of just one variable to plot. I guess that you are looking for:
plot(function(x) 1-pnorm(x),0,3)
Try sorting your data by x, then add the line:
data <- data[order(data$x),]
lines(data$x, data$Px, lwd=2)

can't delete y axis label in plot.gbm

Run-on question following this problem setting axis widths in gbm.plot; I'm now using plot.gbm directly and don't seem to be able to remove the y axis label, which seems to be set within the plot.gbm function code.
png(filename="name.png",width=4*480, height=4*480, units="px", pointsize=80, bg="white", res=NA, family="", type="cairo-png")
par(mar=c(2.6,2,0.4,0.5), fig=c(0,1,0.1,1), las=1, lwd=8, bty="n", mgp=c(1.6,0.5,0))
plot.gbm(my_gbm_model,1,return.grid=FALSE, write.title=F,lwd=8, ylab=F, axes=F, ylabel=FALSE, ylabel="")
axis(1, lwd.ticks=8, lwd=8, labels=FALSE)
axis(2, lwd.ticks=8, lwd=8, labels=NA, ylab=FALSE,ylabel=FALSE)
dev.off()
Result:
The y axis label is still there despite all my atempts to remove it through par and plot and axis. I could try burrowing into the function and changing this (and similar) lines:
print(stripplot(X1 ~ temp | X2 * X3, data = X.new,
xlab = x$var.names[i.var[i[1]]],
ylab = paste("f(", paste(x$var.names[i.var[1:3]], collapse = ","), ")", sep = ""),
...))
...but I've been advised against such practices. Any thoughts why this might be working? Simply that the function overrides the setting?
Reproducibility:
#core data csv: https://drive.google.com/file/d/0B6LsdZetdypkWnBJVDJ5U3l4UFU
#(I've tried to make these data reproducible before and I can't work out how to do so)
library(dismo)
samples <- read.csv("data.csv", header = TRUE, row.names=NULL)
my_gbm_model <- gbm.step(data=samples, gbm.x=1:6, gbm.y=7, family = "bernoulli",
tree.complexity = 2, learning.rate = 0.01, bag.fraction = 0.5)
The problem is that plot.gbm just isn't a very R-like function. Since anyone can submit a package to CRAN it's not required that they follow traditional R patterns and that looks like what happened here. If you step though plot.gbm with your sample data, you see that ultimately the plotting is done with
plot(X$X1, X$y, type = "l", xlab = x$var.names[i.var], ylab = ylabel)
and the ylabel is set immediately before with no option to disable it. The authors simply provided no standard way to suppress the ylab for this particular plotting function.
In this case the easiest way might just be to reduce the left margin so the label prints off the plot. Seems like
par("mar"=c(5,2.2,4,2)+.1, fig=c(0,1,0.1,1), las=1, lwd=8, bty="n")
plot.gbm(my_gbm_model,1,return.grid=FALSE, write.title=F,lwd=8, ylab="", axes=F)
axis(1, lwd.ticks=8, lwd=8, labels=FALSE)
axis(2, lwd.ticks=8, lwd=8, labels=FALSE)

Resources