Evenly distribute X values within a given range - math

This is driving me nuts. I am looking for the formula to evenly calculate x values within a range of minimum and maximum values. For instance...
min = 4
max = 20
x = 3
Should equate to...
= 8, 12, 16
I feel like the answer is right in front of my face, I'm sure I covered it at some point during one of my math or statistics courses, but I just can't puzzle it out. I have looked at similar questions here, but they are all programming specific and aren't really shedding any light for me. I would think there has to be a basic formula for this. Any help would be greatly appreciated.

assuming t = 1..x:
a[t] = min+t*(max-min) / (x+1)

Related

R - Weights? Proportion? About distribution

Sorry, I don't know how to express the title I want to ask.
Because I don't know what the "keyword" is, I can't find a solution.
my question is
Suppose I have a set of 10000 numbers, and its range is from 0~40000
```I use 10 groups as an example.
x <- data.frame(num=c(0,13,58,609,829,2574,6517,12257,16478,19841))
```
I hope he can redistribute a new set of numbers to him based on this data.frame number range.
Assuming a maximum of 20000, the range
1~2000=1, 2001~4000=2, 4001~6000=3, 6001~8000=4, 8001~10000=5,...,18001~20000=10
```
x <- data.frame(num=c(1,1,1,1,1,2,4,6,8,10))
```
Because the real situation is, I don't know what the maximum value is, so I need to find a kit to help.
If my instructions are not clear enough, please let me know
Thank you
We can use findInterval
findInterval(x$num, seq(2001, 20000, by = 2000)) + 1

Simulating realistic noise for a calcium baseline

thanks to the truely amazing community my project group is one step closer to mimic realistic calcium baseline noise.
I simulated a typical calcium movement in a mathematical model:
Thanks to the community I could add random noise to the unrealistic baseline:
However, the noise dynamic is actually too fast. Is there a way to slow down the noise and create broader noise peaks instead of these spikes. I add an actual measurement to show you what I mean:
If this question is too specific, I apologize and will delete the post.
Best wishes and many thanks!
Please make your question and examples reproducible so that others can help. That being said, it looks like the baseline is a just a random normal -- probably created with something like x <- rnorm(500). One way to make this less jumpy is calculate a moving average. You could use a package like TTR or zoo to do this, or you can create your own function. For example:
x <- rnorm(500)
plot(x, type = "l")
ma <- function(x, n = 5){ filter(x, rep(1/n, n), sides = 2) }
plot(ma(x), type = "l")
plot(ma(x, 10), type = "l")
I see your point now. I have two suggestions for this case, maybe they will be of help :
Try to add noise to only a subset of your base line ( following is a 10%)
baseline.index = which(App[,2] == min(App[,2]))
baseline.index.subset = sample(x = baseline.index, size = 0.1 * length ( baseline.index) , replace = F)
noise = rnorm( length (baseline.index.subset))
App[ baseline.index.subset,2] = App[ baseline.index.subset,2] + noise
And try to play a bit with the mean and standard deviation of the noise. ie:
noise = rnorm( length (baseline.index.subset), mean = 0, sd = 0.1)
Let us know if this helps

Function to Create Y-Values

I was hoping someone might be able to help me make sense of a homework question. I am not looking for a solution, mind you, just wondering if anyone would be able to explain the question a bit more simply for me, as I am new to data analysis and enrolled in an R class which had no prerequisites, but feel a bit lost with some of the language. Any help would be greatly appreciated!
So, the first part of the question was to create an array and fill it with random numeric data, which I did here:
question <- array( 1:1000, dim= c(25,4,1000))
colnames(question)<- c('x1','x2','x3','x4')
Now, the second part asks me to "write a function to create y-values," which should be a "linear combination" of the four variables. The example given is
y = 2 ∗ x1 + 5 ∗ x2 − 3 ∗ x3 + 0.7 ∗ x4 + RandomError.
The question adds that the result should be a matrix with dimensions of 25 × 1000. I am not sure what exactly this is asking or how to approach this problem. All I have so far, which I know is very little is
apply(question,c(1,3),sum)
function (y){ ...
Can anyone offer any guidance or clarification? Thank you so much!
First of all, to make (pseudo)random numbers, you can use the rnorm function. That is, if you want to make 1000 random numbers that are normally distributed with mean of 0 and sd of 1, you can do rnorm(1000) (However, your array ends up being length 10000, so maybe you actually want to do rnorm(10000)).
Now, you should have an array question with dimensions 25 x 4 x 1000. You want to create a matrix y which combines four "slices" in question of size 25 x 1000 to create a matrix y of size 25 x 1000. You want to write a function f that will take all four "slices" of array question and combine them into one slice. You also want to incorporate random error, which again can be accomplished with the rnorm function.
For a simple example, let's make an array x with dimensions(10,2,10)
x = array(rnorm(200), dim = c(10,2,10))
And now let's write a function f that will add the two "slices" of x together.
f = function(my_array){
my_array[,1,] + my_array[,2,]
}
Let's execute the function on our array
y = f(x)
dim(y)
Hopefully you can expand this basic example to fit your case.

How to find the average of a set of bearings [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do you calculate the average of a set of angles?
If I have a set of bearings, ranging from 1-360, how can I find the average? Usually to find the average one would add them all up and divide by the number of items. The problem here is that doing that in the case of [1, 359], 2 bearings, would result in in 180, which in fact should be 360. Any ideas?
Represent the angles as vectors with Norm=1 and average the sum.
x1 = {cos(a),sin(a)}
x2 = {cos(b),sin(b)}
(x1+x2)/2 = {(cos(a)+cos(b))/2,(sin(a)+sin(b))/2}
which means the angle for the mean is
atan2((sin(a)+sin(b)) /(cos(a)+cos(b)))
Just beware of controlling the possible overflow when the denominator is close to zero.
It isn't clear from your question what you're trying to define the "average" to be... for directions on a circle there is no clear-cut obvious notion of average.
One interpretation is the value x that is the closest fit to the set of provided values, in the least-squares sense, where the distance between two bearings is defined as the smallest angle between them. Here is code to compute this average:
In[2]:= CircDist[a_, b_] := 180 - Mod[180 + a - b, 360]
In[6]:= Average[bearings_] :=
x /. NMinimize[
Sum[CircDist[x, bearings[[i]]]^2, {i, 1, Length[bearings]}],
x][[2]]
In[10]:= Average[{1, 359}]
Out[10]= -3.61294*10^-15
So what you want is the middle of two bearings - what happens if you have {90, 270}? Is the desired answer 0 or 180? This is something to consider.. also what's the middle of three bearings?
One thing you could do is:
Take the first two bearings in your set
Work out the difference between the two in either direction (i.e. [1, 359] would give 2 degrees in one direction, and 358 in the other)
If you want the desired angle to be the middle of the acutest of the two, take that as your difference and add to the anti-clockwise most of the pair (i.e. 359)
Use this as the new bearing and the next (i.e. 3rd in set) as the other bearing, and repeat, until all are 'middled'.
Off the top of my head, I don't think this is going to be fair, it'll probably bias it in one direction though (i.e. maybe in preference of the later values in your set).

Create curve 'algorithm' after capturing points from user?

I am capturing some points on an X, Y plane to represent some values. I ask the user to select a few points and I want the system to then generate a curve following the trend that the user creates. How do I calculate this? So say it is this:
Y = dollar amount
X = unit count
user input: (2500, 200), (4500, 500), (9500, 1000)
Is there a way I can calculate some sort of curve to follow those points so I would know based off that selection what Y = 100 would be on the same scale/trend?
EDIT: People keep asking for the nature of the curve, yes logarithmic. But I'd also like to check out some other options. It's for pricing the the restraint is that the as X increases Y should always be higher. However the rate of change of the curve should change related to the two adjacent points that the user selected, we could probably require a certain number of points. Does that help?
EDIT: Math is hard.
EDIT: Maybe a parabola then?
The problem is that there are multiple curves that you can fit to the same data. To borrow an example from my old stats book, here is the same data set (1, 1, 1, 10, 1, 1, 1) with four curves:
You need to specify the overall trend to get a meaningful result.
First, you are going to have to have an idea of what your line is or better said, what type of line fits your data the best. Is it linear (straight line) or does it curve (x-squared). Sounds like this is a curve.
If your curve is a parabola, then you will need to solve y = Ax(2) + Bx + c using your three points that the user has chosen. You will need at least 3 points to solve for 3 unknowns.
200 = A(2500)(2) + B(2500) + C
500 = A(4500)(2) + B(4500) + C
1000 = A(9500)(2) + B(9500) + C
Given these three equations, you should be able to solve for A, B and C, then use these to plot a new curve.
The Least Square Fit would give you a nice data matching curve.
This is a rather general extrapolation problem. In your case, fitting a quadric (parabola) is probably the most reasonable course of action. Depending on how well your data fits a quadric, you may want to fit it to more than 3 points (the noisier and weirder the data, the more points you should use).
Depending on the amount and type of data you have, you may want to try LOESS regression.
However, this may not be a good option if you only have 3 points as in your example (but keep in mind that you will not be able to have good extrapolation with 3 points no matter the algorithm you use)
Another option would be B-splines

Resources