R bivariate normal dmvnorm graph - r

So, I'm trying to graph a bi-variate normal centered at 12.5 and 7.5, but it's always off centered. Really, I'm trying to sample from a bi-variate distribution and just graph it real fast to make sure it worked. Also, when I switch mu in dmvnorm to 50's it will work. I know that this is not the best code/graph, but it's formatted this why to try to figure out what happened. I don't know if the mistake is in the sampling or in the graphing. What is going on here?
library(mixtools)
x3<-seq(10,15,by=.05)
x4<-seq(5,10,by=.05)
y<-matrix(NA,nrow=length(x3)*length(x4),ncol=3)
dim(y)
counter<-1
for(i in seq(1,length(x3))){
for(j in seq(1,length(x4))){
#Change 12.5 and 7.5 to 50's will put it in the center
y[counter,]<-c(dmvnorm(y=c(i,j),mu=c(12.5,7.5),sigma=matrix(c(1000,0,0,1000),nrow=2))*1000,x3[i],x4[j])
counter<-counter+1
}
}
plot(y[,2],y[,3],pch=16,col=rgb(0,y[,1],0,maxColorValue=(dmvnorm(y=c(12.5,7.5),mu=c(12.5,7.5),sigma=matrix(c(1000,0,0,1000),nrow=2))*1000)),asp=1,xlim=c(min(y[,2]),max(y[,2])),ylim=c(min(y[,3]),max(y[,3])))

Some comments:
dmvnorm(y=c(i,j)) is wrong, you need to evaluate the density at c(x3[i], x4[j]).
I didn't read your plot() statement that carefully, but plot() only takes at most two variables (you have 3), you'll need to use something like image() or levelplot().
Here's what you are looking for:
library(lattice)
d <- expand.grid("x3" = seq(10, 15, .05), "x4" = seq(5, 10, .05))
d$dens <- dmvnorm(as.matrix(d), mu = c(12.5, 7.5), sigma = diag(1000, 2))
levelplot(dens ~ x3 * x4, data = d)

Related

Perona-Malik model in R for smoothing time series of data

Recently, i use Savitzky-Golay in signal package for smoothing my data, but it is not work well. I hear that Perona-Malik is good smooth method for this task, however, i could not realize it. My question is that is it possible realize the task to smooth the data by using P& M model by using R.
Thanks
hees
Simple example.
library(signal)
bf <- butter(5,1/3)
x <- c(rep(0,15), rep(10, 10), rep(0, 15))
###
sg <- sgolayfilt(x) # replace at here
plot(sg, type="l")
lines(filtfilt(rep(1, 5)/5,1,x), col = "red") # averaging filter
lines(filtfilt(bf,x), col = "blue") # butterworth
p

Graphing non linear equation of two variables in RStudio

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)

Drawing an interval on the graph using vertical line in R ?

install.packages("devtools")
library(devtools)
devtools::install_github("google/CausalImpact")
library(CausalImpact)
set.seed(1)
x1 <- 100 + arima.sim(model = list(ar = 0.999), n = 100)
y <- 1.2 * x1 + rnorm(100)
y[71:100] <- y[71:100] + 10
data <- cbind(y, x1)
pre.period <- c(1, 70)
post.period <- c(71, 100)
impact <- CausalImpact(data, pre.period, post.period)
plot(impact, "cumulative")
Say i want the graph to show an interval from 71-100 with the x scales starting at 1 from the first dotted line any ideas on how to do this?
Does anyone have any idea how to add a second vertical dotted line depicting an interval on the graph? Thanks.
You can use abline() to add lines to a graph, with the argument v = 70 setting a vertical line at x = 70. I'm not sure how to restart the x-scale from that point however - it doesn't seem like something that would be possible but perhaps someone else knows how.
You can reset the axes using this.
In your initial plot command, set xaxt = "n" This ensures that the plot function does not mark the axes.
You can then draw the abline(v=70) as mentioned above.
Then use axis(1,at=seq(60,80,by=1),las=1) 1 stands for x-axis and in the at attribute, mention the x limits you want. I've put in 60 to 80 as an example.

What does autoplot.microbenchmark actually plot?

According to the docs, microbenchmark:::autoplot "Uses ggplot2 to produce a more legible graph of microbenchmark timings."
Cool! Let's try the example code:
library("ggplot2")
tm <- microbenchmark(rchisq(100, 0),
rchisq(100, 1),
rchisq(100, 2),
rchisq(100, 3),
rchisq(100, 5), times=1000L)
autoplot(tm)
I don't see anything about the...squishy undulations in the documentation, but my best guess from this answer by the function creator is that this is like a smoothed series of boxplots of the time taken to run, with the upper and lower quartiles connected over the body of the shape. Maybe? These plots look too interesting not to find out what is going on here.
What is this a plot of?
The short answer is a violin plot:
It is a box plot with a rotated kernel density plot on each side.
The longer more interesting(?) answer. When you call the autoplot function, you are actually calling
## class(ts) is microbenchmark
autoplot.microbenchmark
We can then inspect the actual function call via
R> getS3method("autoplot", "microbenchmark")
function (object, ..., log = TRUE, y_max = 1.05 * max(object$time))
{
y_min <- 0
object$ntime <- convert_to_unit(object$time, "t")
plt <- ggplot(object, ggplot2::aes_string(x = "expr", y = "ntime"))
## Another ~6 lines or so after this
The key line is + stat_ydensity(). Looking at ?stat_ydensity you
come to the help page on violin plots.

Boxplot outlier labeling in R

I want to draw boxplots in R and add names to outliers. So far I found this solution.
The function there provides all the functionality I need, but it scrambles incorrectly the labels. In the following example, it marks the outlier as "u" instead of "o":
library(plyr)
library(TeachingDemos)
source("http://www.r-statistics.com/wp-content/uploads/2011/01/boxplot-with-outlier-label-r.txt") # Load the function
set.seed(1500)
y <- rnorm(20)
x1 <- sample(letters[1:2], 20,T)
lab_y <- sample(letters, 20)
# plot a boxplot with interactions:
boxplot.with.outlier.label(y~x1, lab_y)
Do you know of any solution? The ggplot2 library is super nice, but provides no such functionality (as far as I know). My alternative is to use the text() function and extract the outlier information from the boxplot object. However, like this the labels may overlap.
Thanks a lot :-)
I took a look at this with debug(boxplot.with.outlier.label), and ... it turns out there's a bug in the function.
The error occurs on line 125, where the data.frame DATA is constructed from x,y and label_name.
Previously x and y have been reordered, while lab_y hasn't been. When the supplied value of x (your x1) isn't itself already in order, you'll get the kind of jumbling you experienced.
As an immediate fix, you can pre-order the x values like this (or do something more elegant)
df <- data.frame(y, x1, lab_y, stringsAsFactors=FALSE)
df <- df[order(df$x1), ]
# Needed since lab_y is not searched for in data (though it probably should be)
lab_y <- df$lab_y
boxplot.with.outlier.label(y~x1, lab_y, data=df)
The intelligent point label placement is a separate issue discussed here or here. There's no ultimate and ideal solution so you just have to pick one there.
So you would overplot the normal boxplot with labels, as follows:
set.seed(1501)
y <- c(4, 0, 7, -5, rnorm(16))
x1 <- c("a", "a", "b", "b", sample(letters[1:2], 16, T))
lab_y <- sample(letters, 20)
bx <- boxplot(y~x1)
out_lab <- c()
for (i in seq(bx$out)) {
out_lab[i] <- lab_y[which(y == bx$out[i])[1]]
}
identify(bx$group, bx$out, labels = out_lab, cex = 0.7)
Then, during the identify() is running, you just click to position where you want the label,
as described here. When finished, you just press "STOP".
Note that each outlier can have more than one label! In my solution, I just simply picked the first!!
PS: I feel ashamed for the for loop, but don't know how to vectorize it - feel free to post improvement.
EDIT: inspired by the Federico's link now I see it can be done much easier! Just these 2 commands:
boxplot(y~x1)
identify(as.integer(as.factor(x1)), y, labels = lab_y, cex = 0.7)

Resources