I've plot this graphic to identify graphically high-leverage points in my linear model.
Given the variable "NOMBRES" of the data set which my model uses, I've tried to plot all the points of my graphic but it gets illegible. Here's the code I ran:
> plot(hatvalues(tmodel),residuals(tmodel))
> text(hatvalues(tmodel),residuals(tmodel),labels=DSET$NOMBRES)
So I would like to plot just the points with leverage(hat value) above 0.05 using the label "DSET$NOMBRES".
Identify high-leverage points according to your definition:
hlev <- which(hatvalues(tmodel)>0.05)
Add numeric labels to the graph:
text(hatvalues(tmodel)[hlev], residuals(tmodel)[hlev],
labels=DSET$NOMBRES[hlev])
Related
I used the following lines to plot a heatmap with plotly in R:
plot_ly(data, x , y) %>% add_trace(type='histogram2dcontour')
I then obtained the following plot
The data actually looks like that on a scatter plot
As you can see, I lose a lot of points on the heatmap. I was wondering how I could manually set the scale for the colour of the heatmap for e.g. making it so that the colour changes every time the count increase by 10 instead of 100.
Otherwise, is there a better way to plot and visualize such data?
I've created a heatmap in R based on simulations and plotted it using image.plot() and I have added contour lines by using contour(). I also have a data frame that contains a column for observations in first year and trend size that I have plotted on top of the heatmap using base R plot. Is there an easy way to count the number of points below the 0.5 contour line and about the 0.95 contour line?
Assuming you are plotting a variable called z, you can use something like.
table(z>.95)
I produced a plot with the partimat function on R.
However, the data points on the picture are ruining the plot. Is there anyway to keep the boundaries and to remove the points from the image? Also, I would like to add a legend to associate each number with it's color.
Here is the code:
library(ElemStatLearn);
data(zip.train);
load(zip.train);
zipTrain=data.frame(zip.train);
x=zipTrain[,-1];
y=zipTrain[,1];
library(MASS)
model=lda(y~.,data=x)
projected.data=as.matrix(x)%*%as.matrix(model$scaling)
projected.data=as.data.frame(projected.data)
M1=projected.data
couleurs=c("aquamarine","blue","brown","chartreuse","cyan","darkgreen","darkorange","pink","gold","gray")
library(klaR)
Matrice=data.frame(X2=M1[,2],X1=M1[,1],Y=y)
partimat(x=Matrice[,-3], grouping=as.factor(Matrice[,3]), method="lda",
col.mean="gold", image.colors =couleurs,pre=200,display.points=FALSE)
I am trying to simulate a minefield by plotting two Poisson distributed samples in the same plot, one with a higher intensity and smaller area than the other. This is the minefield and the other is just noise (stones, holes, metal) seen as points. I cannot get R to plot the points with the same units in the axis. Whatever I do, the points span the entire plot, even though I only want the X points to cover a quarter of the plot. My R-code is just the following:
library(spatstat)
Y = rpoispp(c(5),win=owin(c(0,10),c(0,10)))
X = rpoispp(c(10),win=owin(c(0,5),c(0,5)))
Please let me know if you can help me.
My guess is that you are doing something like:
> plot(Y)
> plot(X)
to plot the points.
The problem with this is that the default behavior of the plot function for the class ppp (which is what the rpoispp function returns) is to create a new plot with just its points. So the second plot call essentially erases the first plot, and plots its own points in a differently scaled window. You can override this behavior by setting the option add=TRUE for the second plot. So the code
> plot(Y)
> plot(X, add=TRUE, cols="red")
should get you something like:
Check out the docs (help(plot.ppp)) for more explanation and other options to prettify the plot.
How to plot the density of a single column dataset as dots? For example
x <- c(1:40)
On the same plot using the same scale of the x-axis and y-axis, how to add another data set as line format which represent the density of another data that represents the equation of
y = exp(-x)
to the plot?
The equation is corrected to be y = exp(-x).
So, by doing plot(density(x)) or plot(density(y)), I got two separated figures. How to add them in the same axis and using dots for x, smoothed line for y?
You can add a line to a plot with the lines() function. Your code, modified to do what you asked for, is the following:
x <- 1:40
y <- exp(-x)
plot(density(x), type = "p")
lines(density(y))
Note that we specified the plot to give us points with the type parameter and then added the density curve for y with lines. The help pages for ?plot, ?par, ?lines would be some insightful reading. Also, check out the R Graph Gallery to view some more sophisticated graphs that generally have the source code attached to them.