Let's take sample 'y' of length 100 from N(0,1) distribution and function g(x)=x^2+7.
I want to make a graph of function $h(x)=\sum_{i=1}^{100}g(x-y[i])$
So
y=rnorm(100,0,1)
g=function(x){x^2+7}
h=function(x){sum(g(x-y))}
And now expressions like
curve(h)
plot(h)
lines(h)
doesnt work, can You please help me how can i draw graph of function h?
Related
I have a dataset called mypoints and I have created a polygon and plotted the points as below:
mypoints=read.csv("d:\\data\\venus.csv",header = T)
mypoints
minx=min(mypoints[,1])
maxx=max(mypoints[,1])
miny=min(mypoints[,2])
maxy=max(mypoints[,2])
mypolygon=cbind(c(minx,maxx,maxx,minx),c(miny,miny,maxy,maxy))
plot(mypoints)
polygon(mypolygon)
I now want to write a function that calculates both the mean center and standard distance for mypoints. I then need to plot the standard distance as a circle centered on the mean center of all points with the radius equal to the standard distance. Note that the last expression evaluated in a function becomes the return value, the result of invoking the function.
So far:
#I think this how I calculate the mean center for x and y:
x1=sum(mypoints[,1])/length(mypoints[,1])
y1=sum(mypoints[,2])/length(mypoints[,2])
#This is the formula I was shown for standard distance:
sd.mypoints=sqrt(sum(x1+y1)/n)
#This is the formula I was shown for creating the circle:
symbols(sd.mypoints[1],sd.mypoints[2],sd.mypoints$sd,add=T,inches=F)
#This is the error that I get when I run the circle formula:
Error in sd.mypoints$sd : $ operator is invalid for atomic vectors
I have found it easier to find the Nearest Neighbor, do KDE, Ghat, and Fhat for this dataset than trying to figure this out. I am sure there is a easy solution for this but I just can't seem to get it. Third class in R and it has been a lot of fun up to this point.
You have the line
symbols(sd.mypoints[1],sd.mypoints[2],sd.mypoints$sd,add=T,inches=F)
in your code. As said in the comments, sd.mypoint is not a data.frame, so subsetting it with sd.mypoint$sd` causes the error you see.
From the documentation of symbols, which you can access with ?symbols you'll see that for circles the circles argument is mandatory, so the function can differentiate what sort of figure is drawing.
EDIT:
Also, please notice that you are using x and y points to symbols different to the ones you already calculated. So you need to replace that line with:
symbols(x1, y1,circles = sd.mypoints,add=T,inches=F)
Notice the use of x1 and y1. I can see the plot now.
I have a matrix with 4 variables whereas 3 variables are parameters and the 4th variable gives the mean sum of squares for simulation results with the corresponding variables. Now I'd like to create a ternary plot with R where the triangle corresponding to the 3 parameter values should be colored by the mean sum of squares value. Alternatively, I'd like to plot interpolated mean sum of squares in the whole simplex triangle.
I was already looking for some functions or code that does what I'm looking for. But I didn't succeed.
Nevertheless, here's an example code of how my data set looks like (for which I'd like to create the ternary plot):
grid <- as.matrix(expand.grid(seq(0,0.5,0.025), seq(0,0.5,0.025), seq(-0.25,0.25,0.025)))
data <- cbind (grid, runif(9261,0,2))
I'd be very thankful if you'd provide R code that can create the plot I'd like to get. Maybe there's even a pre-implemented function in a package that I haven't found?!
Thanks a lot in advance for your help!
I calculated the cumulative probability (in English, cdf) of my data, based on the probability of exceedance (edf). No problem at all.
However, does anyone know if there is any command to transform this data into probability density (pdf)?
I have already tested using the histogram function, but it does not work correctly.
x <- c (0.00000000, 0.03505324, 0.07005407, 0.10512053, 0.14021308,
0.17533767, 0.21051443, 0.24570116, 0.28090087, 0.31592221,
0.35092739, 0.38591441,0.42085712, 0.45599341, 0.49119521, 0.52646341,
0.56159558, 0.59673546, 0.63172464, 0.66674853, 0.70177413, 0.73712542,
0.77225123, 0.80750715, 0.84250460, 0.87720473, 0.91172191, 0.94588810,
0.98056348)
Is the function you are looking for density() ?
You can plot with
plot(density(x))
You can see x and y values with:
density(x)$x
density(x)$y
I want to calculate the following integrate by using the hit and miss method.
I=∫x^3dx with lower= 0 and upper =1
I know how to solve it but I cannot find the right code in R to calculate it and generate -for example 100000 random- and then plot them like this:
Thank you.
1. Generate 2 vectors from uniform distribution of the desired length
l = 10000
x = runif(l)
y = runif(l)
2. The approximation of the integral is the number of cases where the (x,y) points are below the function you want to integrate:
sum(y<x^3)/l
3. For the plot, you just have to plot the points, changing their color depending whether they are above or below the curve, and add the function with curve():
plot(x,y,col=1+(y<x^3))
curve(x^3,add=T,col=3)
I am doing some caclulations with extreme velocities and the only way to solve my system equations is to do it graphically. Once I have plotted my curve, I would like to develop a function that enter an x-value and the function itself plots a line from this x-value up to the corresponding point of the curve and from this point, another line over y-value. Like this I would get my y-value that would be the solution of my system equations.
Here is my code. The function Vr_Vmed is the expression of my final equation. In fact, n=4 and Tr=50 and x is the variable.
par(font=10,font.axis=10,font.lab=10,font.main=11,font.sub=10)
curve(Vr_Vmed(x,n,Tr),xlim=c(1,2.5),ylim=c(1,17),
xaxs="i",yaxs="i",xaxt="n",yaxt="n",lwd=2,
xlab="K Weibull",ylab="Vref / Vmed",usr=c(1,2.5,1,17),
main="Vref Estimation")
axis(1,at=c(seq(1,2.5,0.1)),xaxp=c(1,2.5,1))
axis(2,at=c(seq(1,17,1)))
If you just want to add lines to your plot,
you can use lines or segments.
f <- function(x) {
y <- Vr_Vmed(x,n,Tr)
lines(c(x,x,0),c(0,y,y))
}
f(2)
(But that does not "solve" anything: your Vr_med function
aparently does all the work.)