Plot function in R with two input variables [duplicate] - r

This question already has answers here:
3D equivalent of the curve function in R?
(4 answers)
Closed 8 years ago.
I have the following function in R:
n<- function(theta){
d=theta[1]
z=theta[2]
Nh= c(1819, 1018)
N= sum(Nh)
sigmah= c(0.013, 0.0155)
n=sum(Nh*sigmah)^2/(N^2*d^2/z^2+sum(Nh*sigmah^2))
return(n)
}
I would like to plot the function n over a range of inputs for d and z. How could I do this?
Thanks

I don't know what range of values is appropriate, but this produces something not too crazy:
library("emdbook")
curve3d(n(c(x,y)),xlim=c(1,2),ylim=c(1,2))

Related

Simple moving averages with formula [duplicate]

This question already has answers here:
Calculating moving average
(17 answers)
Closed 3 years ago.
I have to calculate Simple Moving Averages on some data with this formula in R:
Does anybody know how to do that?
Are you looking for something like that?
Func <- function(xt){
St <- 1/10*sum(sapply(0:9, function(z) xt-z))
return(St)
}
And the output for two example of Xt:
> Func(10)
5.5
> Func(15)
10.5

how to divided a vector and get the partition num in R? [duplicate]

This question already has answers here:
Dividing a vector into categories
(1 answer)
How to create a categorical variable in R with unequal categoris [closed]
(1 answer)
R if else with for loop [duplicate]
(3 answers)
Closed 5 years ago.
I'm programming in R.I need to divided a vector into x partitions(eg,x=4),and get the partition number of the vector, something like this...
a <- data.frame(x=1:20)
a$numofpartition<-ifelse(a$x<6,1,ifelse(a$x<11,2,ifelse(a$x<16,3,4)))
As the code,I divided a$x into 4 partitions,and get the partition number for each x, any functions in R could do this? Thank you!

How to calculate cumulative mean in R? [duplicate]

This question already has answers here:
Calculate cumulative average (mean)
(7 answers)
Closed 5 years ago.
(I am sorry if the term is not correct).
In R, I have a numeric vector x. I want to create new vector y where:
y[i] = mean (x[1:i)
It is easy to write a function to calculate y, but is there a built-in function in R which do the task?
Thank you very much
Try this
y <- cumsum(x) / seq_along(x)
Reference
https://stat.ethz.ch/pipermail/r-help/2008-May/162729.html

How do i take the last n elements from a vector in R? [duplicate]

This question already has answers here:
Getting the last n elements of a vector. Is there a better way than using the length() function?
(6 answers)
Closed 5 years ago.
suppose I have daily time series data under variable name "prices", but im only interested in the past 100 days. How would i extract the last 100 elements from this variable?
Something equivalent to python's prices[-100:] but for R?
If it's a vector:
tail(prices, 100)

Transform scaled numbers to raw numbers in R [duplicate]

This question already has answers here:
backtransform `scale()` for plotting
(9 answers)
Closed 8 years ago.
I'm new in R. I would like to transform a set of numbers I have scaled using scale() to the original raw ones.
Here the code I used to scale the numbers
dataCluster <- dataFinal[, c(1)]
data_z <- as.data.frame(lapply(dataCluster, scale))
clusters <- kmeans (na.roughfix(data_z), 3)
where:
dataFinal is a data frame (3 columns x 100 rows)
clusters is a "data matrix" (3 columns x 3 rows).
I would like to create a clustersRaw with the raw values.
Can anyone help?
Don't know it this is going to solve, since you dind't provide your data. However:
#create a matrix 10x3
mat<-matrix(1:30,ncol=3)
#scale it
x<-scale(mat)
#restore it
t(t(x)*attr(x,"scaled:scale")+attr(x,"scaled:center"))

Resources