Hist of circular residual in R programme between -pi and pi [closed] - r

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have the data of error, firstly I need to transform the range of data between -pi and pi
then using code hist(error)
my question is how I can transform this data using code in R, if there exists code please tell me?
error<- c( 5.71444784 ,5.55435896 ,5.60671884, 5.19927462 ,4.81470000 ,5.53028500,
0.87085808 ,5.37982604, 5.43760222 ,5.77523327, 5.68796681 ,5.54533123,
5.27149485 ,5.75717780 ,0.53623627 ,5.29496664 ,5.33288247 ,5.49297135,
5.51343389 ,4.87307837 ,4.87849468 ,5.78305665, 0.13721761 ,5.91185037,
5.50741540 ,5.72588264, 5.03918574, 4.14846564 ,5.25644862 ,0.57956841,
0.37614739, 0.40864692, 5.92087811, 5.92689660, 5.72889189 ,5.64643955,
5.96902437, 5.91666449, 6.18508456 ,5.86249974 ,5.17279359, 5.37982604,
5.25163450, 5.43098155, 5.73912232, 5.80592625 ,5.43940710, 4.84659734,
5.76078923, 5.76379847, 5.76078923 ,5.78606589, 5.68977169 ,5.68375320,
0.42790509 ,6.08939254, 4.89955940 ,5.53389643, 5.67954126 ,5.71324348,
5.66930915 ,5.54111761 ,5.87273017, 5.86791605 ,5.50199909, 5.50199909,
5.69699455 ,5.29737370 ,4.97117745, 5.62838408 ,5.77402891, 5.30640144,
5.85106494 ,5.58926555 ,5.58926555 ,4.62271379 ,3.36547454, 6.19892642,
0.28888093 ,6.09541103, 5.89499926 ,5.87453672, 5.67954126 ,5.46408326,
1.44982681 ,0.24193736 ,0.77516606 ,5.88055521, 5.55435896, 5.58926555,
5.58926555 ,5.39126084, 4.47466189, 0.06800662, 5.75777998, 5.44963921)
I try this problem by transforming the data handly .... and hist ... but the residual doesn't about zero.

To compute the range from -pi to pi just subtract pi from the radian measurement since radians range from 0 to 2*pi:
error - pi
hist(error - pi)
But that does not give you what you want. You really need to use circular statistics to see what is going on:
install.packages("circular")
library(circular)
plot.circular(error, stack=TRUE)
You can ignore the warning message. Notice that the data are not around zero. To plot a histogram we need to cut the data at pi and flip the values greater than pi:
errortrans <- ifelse(error<pi, error, error-2*pi)
# Alternate transform
# errortrans <- ifelse(error<pi, -error, 2*pi-error)
hist(errortrans, xlim=c(-pi, pi))

Related

Coding a plot with functions [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
So I've probably referenced the entire internet trying to make this problem work, and haven't. However, I found stack overflow. Like I said I've been learning for not even 2 weeks yet.
So this is the problem
Let
f(x)=sqrt((x^3+3x^2+1)/(x^4+5x^3+7x+9))
(x ≥ 0)
(a) Draw a line graph of (x, f(x)) for 0 ≤ x ≤ 10 with increments of 0.01
(b) Find numerically the maximum value of f(x) and the maximizer x (report x to the
second decimal place. For instance, x = 1.23)
So I'm basically been saying x=x and y= the sqrt....., and then I write plot(x,y,type="l") and usually it just doesn't even work.
Also how do I do the increment part. I'm sorry for lack of explanation, but I have no idea what most of this means.
First thing to do would be to define the function:
equation <- function(x){
sqrt((x^3+3*x^2+1)/(x^4+5*x^3+7*x+9))
}
Then, define the values you want to apply the function to, and store them in vector input
input<-seq(0,10,0.01)
Apply the equation function to input, and store the values in vector results
results<-sapply(input,function)
Produce a line plot:
plot(input,results,type="l")
Print the value of x which maximises f(x)
maxx<-input[which.max(results)]
maxx
I would suggest a ggplot2 approach. First you have to create a random x variable and then compute y. I will add the code for that variables an the plot.
In the case of finding the maximum of f(x) you must know calculus or you can use a visual approach. Here the code:
library(ggplot2)
library(dplyr)
set.seed(123)
First we create a random variable x with the limits you mentioned:
#Data
x <- runif(100,0,10)
Now, we save the variable in a dataframe and compute y:
#Allocate data in a dataframe
df <- data.frame(x=x)
#Compute variable
df$y <- sqrt(((df$x^3)+3*((df$x)^2)+1)/((df$x^4)+5*(df$x)^3+7*(df$x)+9))
Finally, we plot:
#Plot
ggplot(df,aes(x=x,y=y))+
geom_point()+
scale_x_continuous(limits = c(0,10))
Output:
Values for x are randomly generated, if you have real values for x you should use those values.

Find x and y coordinates where a perpendicular point crosses a straight line [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
This is a follow-up question to this question.
Taking the following image as an example:
What I know:
x and y coordinates of points D, E, and P.
Therefore, I also know slope and intercept of D-E line
What I want to know:
x and y coordinates of point Q. (This is the point which crosses the D-E line).
Notation P=[px,py], D=[dx,dy], E=[ex,ey], Q=[qx,qy]
First:
R=P-D=[px-dx, py-dy]=[rx,ry]
K=E-D=[ex-dx, ey-dy]=[kx, ky]
Then
z=dot(R,K)/dot(K,K)=(rx*kx+ry*ky) / (kx*kx+ky*ky)
Finally
Q=D+z*K=[dx+z*kx, dy+z*ky]
The R is vector which start on point D and ends on point K, the K is vector which start on point D and ends on point E. Using this we made scalar projection to calculate result Q. More info about concept here

Getting theta of Line Equation [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
Please forgive my lack of knowledge, which i think it's one of those basic formula related to Trigonometry.
Let's look at visual example:
I have 5 lines, with their line equation (let's say they have zero offset ok)
how can i calculate the Theta of each line equation make (in Pi)?
also I have seen this:
Are they generated from Theta of line equations? or it's another theory which help to find the theta?
much appreciate your time and effort
For equation
y = k * x
tg(Theta) = k
and
Theta = Arctg(k) //arctangent function
General line equation
A * x + B * y + C = 0
(It is more general than y=ax+b and includes cases of vertical and horizontal lines)
Theta = atan2(A, B)
(function atan2 or ArcTan2 exists in math libraries of many programming languages)

Creating a Polygon in r -- without connected vertices [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am trying to create a polygon out of this line graph -- is it possible to do without having connected vertices? If so, how do I change the polygon code to do so?
I'm assuming the reason you what to create a polygon is to shade or color within its boundaries. If I'm wrong about that assumption then you really should put more effort into making your questions explicit. The trick is to connect at the ends of those lines by putting them both in one vectors, at the same time as reversing the X and Y for one of the series.
x <- 1:100
y1=6 + rnorm(100)
y2 = rnorm(100)
plot(x, y1, ylim=c(-3,10) )
?polygon
polygon(x= c( x, rev(x) ), y=c(y1,rev(y2) ), col="red")

Dataset with NaN values for a filled contour plot with R [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I cannot get my data plotted properly with R. I got measurements from a football field and I did not fill measurements for each grid.
Here is my dataset contour_map_R.csv at https://db.tt/1L7cxilB
It looks like this using image function to plot it.
Any one can provide an example to create a filled contour plot?
Thanks a lot!
As stated in the comments you need to have complete data before you can calculate contours. Therefore you have to interpolate or replace your missing values in some way that makes sense in your case. I've provided a couple of options below, but you'd need to come up with rationale for using one method over another, and whether a more sophisticated geostatistical approach might be warranted. Furthermore you could interpolate to a finer grid than you currently have as well to produce a smoother result (at the expense of potentially making up data).
d <- read.csv("contour_map_R.csv")
library(raster)
r <- raster(as.matrix(d))
contour(r)
v <- getValues(r)
xy <- xyFromCell(r, 1:ncell(r))
## Interpolate using a thin-plate spline:
library(fields)
tps <- Tps(xy, v)
tp <- interpolate(r, tps)
plot(tp)
contour(tp, add=T)
## Alternatively, interpolate using nearest idw():
library(gstat)
dxy <- data.frame(x=xy[,1], y=xy[,2], v)
dxy <- dxy[complete.cases(dxy),]
id <- gstat(formula = v~1, locations = ~x+y, data=dxy)
ip <- interpolate(r, id)
plot(ip)
contour(ip, nlevels=5, add=T)
If that's what you were looking for you can get filled contours by using the filledContour() function on the interpolated rasters (tp or ip).

Resources