I have basic knowledge in R, I would like to know how to write a code of an exponential function in R
F(X)=B(1-e^-AX)
where A=lambda parameter, B is a parameter represents the Y data, X represents the X data below.
I need the exponential model to generate the curve to fit the data; for example:
X <- c(22, 44, 69, 94, 119, 145, 172, 199, 227, 255)
PS: this x-axis in numbers (in millions).
Y <- c(1, 7, 8, 12, 12, 14, 14, 18, 19, 22)
This y-axis
any idea of how to write the code and fit this model in the data...?
In R you can write an exponential function with exp(), in your case:
F <- Y*(1-exp(-A*X))
Related
The function should perform as follows: The function takes the
arguments: x1, x2, alt = "two-sided", lev = 0.95, where the equality
indicates the default value.
•The arguments x1 and x2 are the X1 and X2 samples, respectively.
•The argument alt is the alternative hypothesis whose two other
possible values are "greater" and "less".
•The argument lev is the confidence level 1 −α. ii. The function
returns an R list containing the test statistic, p-value, confidence
level, and confidence interval.
iii. Inside the function, two Shapiro-Wilk tests of normality are
conducted separately for the two samples (note the normality
assumption at the beginning of the problem). If one or both p-values
are less than 0.05, a warning message is printed out explaining the
situation.
Here is what I have come up with so far but not sure how to create one function to run both:
library(stats)
x1 <- c(103, 94, 110, 87, 98, 102, 86, 98, 109, 92)
x2 <- c(97, 82, 123, 92, 175, 88, 118, 81, 165, 97, 134, 92, 87, 114)
var.test(x1, x2, alternative = "two.sided", conf.level = 0.95)
shapiro.test(x1)$p.value < 0.05|shapiro.test(x2)$p.value < 0.05
Some hints:
Your task is to write a function, so you should have something like this:
my_function <- function(x1, x2, alt = "two-sided", level = 0.95){
# fill in the body of the function here
}
You can do whatever you need to do in the body of the function.
Recall that in R, the last evaluated line of a function is automatically its returned value. So, you might choose to have your last line be list(...) as described in the problem statement.
It will be useful to store results of tests, etc. as variables inside your function, e.g. test_output_1 <- ... so that you can reference those things later in the body of your function.
I have generated two survival curves (Kaplan-Meier estimate) using the function survfit for R from the survival packagem, with a survival object of the form Surv(time_1, time_2, event) and the formula Surv(time_1, time_2, event) ~ gender.
I would like to perform a statistical test of equality of the two resulting survival curves.
Unfortunately such a form of survival object is not admissible for survdiff. It only accepts Surv(time_2, event) which gives different (and in my case wrong) results.
Is there a function which allows me to compare the two curves based on the results of survfit?
Here is the code to create sample data:
e<-c(1, 0 ,1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1)
t1<-c(35, 35, 34, 35, 35, 35, 34, 35, 35, 35, 34, 35, 35, 35, 34, 35)
t2<-c(36, 37, 37, 36, 36,37, 35, 36, 36, 37, 37, 36, 36, 37, 35, 36)
g<-c("F","F","F","F","F","F","F","F","M","M","M","M","M","M","M","M")
data<-cbind(g,t1,t2,e)
data<-data.frame(data)
#result differs
km<-survfit(Surv(time_1,time_2,event)~Gender,data=data_test)
km2<-survfit(Surv(time_2,event)~Gender,data=data_test)
From what I got reading up a bit on the subject, the usual logrank test is not defined for interval censored data. This explains why the survdiff function is complaining about right-censored data.
Nonetheless, there exists generalizations of the logrank for interval censored data. Some seems to be implemented in the interval package (described here).
I cannot really help you more as I only work with right censored data so I never needed these generalization.
I hope that this can help you anyway.
I'm trying to calculate the percentiles from 1:i in a column. For example, for the nth data point, calculate the percentile only using the first n values.
I have tried using quantile, but can't seem to figure out how to generalize it.
mydata <- c(1, 25, 43, 2, 5, 17, 40, 15, 12, 8)
perc.fn <- function(vec, n){
(rank(vec[1:n], na.last=TRUE) - 1)/(length(vec[1:n])-1)}
In Maple I have two lists
A:=[seq(i, i=1..10)];
B:=[10, 25, 43, 63, 83, 92, 99, 101, 101, 96];
Is it possible to do polynomial or power regression in Maple?
I want to fit a trend line as a 3rd order polynomium where each point is (A[i], B[i]).
All you need is
Statistics:-LinearFit([1,x,x^2,x^3], A, B, x);
I'm pretty new to statistics and I need your help. I just installed the R software and I have no idea how to work with it. I have a small sample looking as follows:
Group A : 10, 12, 14, 19, 20, 23, 34, 41, 12, 13
Group B : 8, 12, 14, 15, 15, 16, 21, 36, 14, 19
I want to apply t-test but before that I would like to apply Shapiro test to know whether my sample comes from a population which has a normal distribution. I know there is a function shapiro.test() but how can I give my numbers as an input to this function?
Can I simply enter shapiro.test(10,12,14,19,20,23,34,41,12,13, 8,12, 14,15,15,16,21,36,14,19)?
OK, because I'm feeling nice, let's work through this. I am assuming you know how to run commands, etc. First up, put your data into vector:
A = c(10, 12, 14, 19, 20, 23, 34, 41, 12, 13)
B = c(8, 12, 14, 15, 15, 16, 21, 36, 14, 19)
Let's check the help for shapiro.test().
help(shapiro.test)
In there you'll see the following:
Usage
shapiro.test(x)
Arguments
x a numeric vector of data values. Missing values are allowed, but
the number of non-missing values must be between 3 and 5000.
So, the inputs need to be a vector values. Now we know that we can run the 'shapiro.test()' function directly with our vectors, A and B. R uses named arguments for most of its functions, and so we tell the function what we are passing in:
shapiro.test(x = A)
and the result is put to the screen:
Shapiro-Wilk normality test
data: A
W = 0.8429, p-value = 0.0478
then we can do the same for B:
shapiro.test(x = B)
which gives us
Shapiro-Wilk normality test
data: B
W = 0.8051, p-value = 0.0167
If we want, we can test A and B together, although it's hard to know if this is a valid test or not. By 'valid', I mean imagine that you are pulling numbers out of a bag to get A and B. If the numbers in A get thrown back in the bag, and then we take B, we've just double counted. If the numbers in A didn't get thrown back in, testing x =c(A,B) is reasonable because all we've done is increased the size of our sample.
shapiro.test(x = c(A,B))
Do these mean that the data are normally distributed? Well, in the help we see this:
Value
...
p.value an approximate p-value for the test. This is said in Royston (1995) to be adequate for p.value < 0.1
So maybe that's good enough. But it depends on your requirements!