# Check whether a new value is coherent with the previous ones in R

Hi guys,

I'm new to R and I'm trying to learn It by working on a small project.

My goal is to say whether a new value of blood pressure is "coherent" with the previous ones the patient has inserted or it is just a "random value".

The sample vector I'm working with is the following:

``````pMax <- c(119,120,121,122, 123,124,
119,120,121,122,123,124,
119,120,121,122,123,124,
119,120,121,122,123,124,
119,120,121,122,123,124,
120,120,120,120,120,120,120,120,120,120,120,120,120'
110, 110, 110,
140, 140, 140, 140)
``````

Let's say that I assumed the "normal pressure values" are within the range 119-124 and the values 110 and 140 has been inserted with the only purpose of distract the script.

I tried many ways to achive so and I'm looking for any statistical/R advise.

With standard deviation

``````error <<- sd(pMax)*sqrt((length(pMax)-1)/length(pMax))
print(paste( "Interval pMax ", (mean(pMax, trim=0.5)-error ), " - ", ( mean(pMax, trim=0.5)+error) ))
``````

which gives back
`"Interval pMax 113.877092194063 - 126.122907805937"`

Don't like at all

Percentils

``````sort(pMax)
print( quantile(pMax, c(0.05, 0.95)) )
``````

which gives back

``````    5%    95%
114.05 140.00
``````

Don't like . I may reduce the percentils according to my needs but it cannot be applied to each patient I'm working with.

T-test
`print( t.test(pMax, mu=pNewValues, conf.level = 0.98) )`

gives back

``````	One Sample t-test

data:  pMax
t = -1.2576, df = 49, p-value = 0.2145
alternative hypothesis: true mean is not equal to 123
98 percent confidence interval:
119.7964 124.0036
sample estimates:
mean of x
121.9
``````

and this results looks acceptable.
The range it gives back is at 98% confidence is the same I expect (eventually, I'll round both values to 119 and 25).

pNewValue is a random new value I insert into the script to manually check whether it is "coherent" with the previous one or not.

Now a couple of questions:

1. I can't get why for values higher than the average got from the t.test() - reaching the upper limit of the range - p-value tends time by time to 0 (0.0004386) whereas going towards the lower limit tends to 1. Shouldn't it that - for values closer to the average - the value is supposed not to be "random" and p-value <0.005?
2. t.test() as far as I read is applied to only chisquared distributions and not to normal distribution. am I asked to check first the "skew" both sides before proceeding?

Thank you for any help or any discussion!

N.

Hey @nipa

I think that there are more simple ways of approaching the problem.

Unless you are interested in trends towards the limits, you could compute a tolerance interval for your reference data and check to see if new values are outside of that.

If tends, are important, there are a number of change-point models in R that you can use to try to estimate when your data are shifting.

That's not the case. The test conducted by `t.test()` does not assume that the data are Chi-squared.

Also, there isn't much theoretical legitimacy behind that use of a t-test.

Hi @Max,

I didn't know such a function and I'll try as soon as tomorrow morning to check whether it fits to my needs or not.
Just I'm a bit concerned because menawhile "I know" how t-test or any other statistical methos is performed, I don't know so much about this function.
My aim is - indeed - work with R as much as I can to develop something smart and then migrate to Java for platform-needs.

Paradoxically is the function that gave me back the expected result.

I'll be back as soon as I test the tolerance interval, thank you for the moment

Furthemore, just for my information, how would you approach the trend towards the limit you have been talking about?

I tried the solution suggested by @Max but I don't like the result:

``````> normtol.int(x = pMax, alpha = 0.05, P = 0.95, side = 2)
alpha    P x.bar 2-sided.lower 2-sided.upper
1  0.05 0.95 121.9      107.1687      136.6313
``````

With side=1 the result differs by a couple of numbers but that's not the point.

At the moment the t-test result is the one I find more "logic" and expected...