how to modify argument in function? [closed] - r

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 4 years ago.
Improve this question
I have to solve this:
Modify the cumulative risk calculator of exercise 1 so that the
argument t has the value 5 if user does not give it any other value.
cumulative risk = 1-exp(-1/10000*t).
where the exercise 1 was
We can estimate the cumulative risk of an certain event using the
exponential formula 1-exp(-1/10000*t) where t is the time to the
event. Create a function ans(t), which returns the risk at time t.

To assign a pre-specified value to t you can use
function(t=5){
answer<-1-exp(-1/10000*t)
return(answer)
}

You can define a variable as 5, then call the function using that variable, and save the result somewhere, i.e.
function.exp <- function(t) {
answer <- 1 - exp(-1 / 10000 * t)
return(answer)
}
x = 5
m <- function.exp(x)
m
Result
[1] 0.000499875
Hope that helps :)

Related

How to save a calculation to a variable using variables that have not been defined yet? [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 last month.
Improve this question
Let's say I have a script, where I have a calculation like this:
calculation = c(
a*b +
c*d +
e*f
)
And then in another script I want to call that calculation using the source command.
I get an error saying "Object 'a' not found". What am I doing wrong?
Edit: I don't want to make a function, because this specific calculation is used as input in a complex program in r (apollo) - the input specifies a utility function in a logit regression.
You can capture your expression using expression(), then when you're ready, evaluate using eval():
calculation <- expression(a*b + d*e + f*g)
a <- 1
b <- 2
d <- 3
e <- 4
f <- 5
g <- 6
eval(calculation)
# 44

Generating new variable that removing values less than 0 and greater than 1 [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 4 years ago.
Improve this question
Currently, I have generated variable pscore <- reg1$fit and created a overlapping histogram with the following code:
hist(pscore[data$smoke==1],breaks=40,col=col.alpha("red",.5),freq=FALSE,
xlim=c(-0.025,0.529),ylim=c(0,10),xlab="Propensity Score",ylab="",main="")
hist(pscore[data$smoke==0],breaks=20,col=col.alpha("blue",.5),freq=FALSE,
ylim=c(0,9),add=TRUE)
Mind that variable smoke is binary
I'm now trying to create another histogramm but with pscores between 0 and 1
pscore2 <- reg1$fit>=0 & reg1$fit <=1
R will read it but when running the histogram code with the new pscore, it results in an error.
Error in hist.default(pscore2[data$smoke == 1], breaks = 20, col = col.alpha("blue", :
'x' must be numeric
Error in hist.default(pscore2[data$smoke == 0], breaks = 20, col = col.alpha("blue", :
'x' must be numeric
So not sure what other way I could create a new pscore that only has values between 0 and 1.
You should do it like this:
pscore2 <- reg1$fit[reg1$fit >=0 & reg1$fit <=1]
Hope it helps.

random walk function in 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 8 years ago.
Improve this question
I have a random walk function
random.walk = function(n){
return(cunsum(c(0, sample(c(-1, 1), size = n-1, replace =TRUE))))
}
I would like to make recursive random walk function.
what I did was
recursive.rwalk=function(n){
return(random.walk(n-1) + random.walk(n))
}
but then Im getting warning message.
Please help me out!
Thanks
You are trying to add two vectors of different lengths: n-1 and n in this line: random.walk(n-1) + random.walk(n). R is warning you that the first element of the first vector is added to the last element of the second vector (value recycling).
Altough it's obviously not a good idea to calculate a random walk recursively in R, here is how you would do that. First of all you have to recursively call the recursive.rwalk function, not the random.walk function. Next, you still need to sample each step of the way. And finally you need to tell the function when to stop recursively calling itself. Here's one way that will work:
recursive.rwalk=function(n){
if (n==0) return(0)
rw <- recursive.rwalk(n-1)
return(c(rw, rw[n-1]+sample(c(-1,1), 1)))
}
set.seed(123)
recursive.rwalk(5)
## [1] 0 1 0 1 2

logstar function_the iterate of the natural logarithm_r [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In R, I would like to create a function that returns the smallest n such that the n-th repetition of the natural logarithm gives a value smaller than one. Ex.: fun(9182) = 3 because ln(ln(ln(9182))) = 0,793 < 1.
Any suggestions will be appreciated!
logstar<-function(x){if (x<1) 0 else 1 + logstar(log(x))}
#mrip's answer works well for single values. If you'd like a function that works for vectors, you'll want to use ifelse() rather than if:
> logstar <- function(x){ifelse(x<1,0,1 + logstar(ifelse(x<1,x,log(x))))}
> x = c(0.5,1,100,10000,1E8)
> logstar(x)
[1] 0 1 3 3 4
The ifelse() in the recursive call to logstar() prevents log() from generating NaN in some cases.

how to do looping in 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 9 years ago.
Improve this question
Hey guys i want to do looping in R ; can anybody help me out
For eg i have sum of points and i want no of empid in 0-10 % of sum of points and so on ;how to do in R
For eg I have data as
empid sumofpoints
1 10
2 30
I want data as
percentageofsumpoints countofempid
0-10 4
11-20 5
21-30 6
and so on....
How to do it in R ,do i have to install any package for it
No need to install package.See http://nunn.rc.fas.harvard.edu/groups/pica/wiki/1f131/
Simple for loop
for (i in 1:10){
print(i)
}
In your example, asssuming your data is stored in a dataframe called df
res <- NULL
groups <- c(0,10,20,30,40,...)
for (i in 2:length(groups)){
res <- rbind(res,c(paste(groups[i],groups[i-1],sep="-"),nrow(df[df$sumofpoints <= groups[i] & df$sumofpoints > groups[i-1],])))
}
You can also use apply functions if you want to avoid for statements. This example I have taken directly from the help files
x <- cbind(x1 = 3, x2 = c(4:1, 2:5))
dimnames(x)[[1]] <- letters[1:8]
apply(x, 2, mean, trim = .2)
EDIT further to this how to avoid loops
For large dataset, refer to the package foreach. This allows for a sequential loop set-up using %do% or a parallel set-up (faster for large datasets) using %dopar%.
http://cran.r-project.org/web/packages/foreach/vignettes/foreach.pdf
For parallel computing, be mindful that you will need a backend such as "doParallel" or "DoSNOW". There is also "doMC" which only works with operating systems that support the
fork system call (which means that Windows isn't supported).

Resources