Displaying 20 results from an estimated 100 matches similar to: "integrate: Don't do this?"
2007 Aug 22
3
integrate
Hi,
I am trying to integrate a function which is approximately constant
over the range of the integration. The function is as follows:
> my.fcn = function(mu){
+ m = 1000
+ z = 0
+ z.mse = 0
+ for(i in 1:m){
+ z[i] = rnorm(1, mu, 1)
+ z.mse = z.mse + (z[i] - mu)^2
+ }
+ return(z.mse/m)
+ }
> my.fcn(-10)
[1] 1.021711
> my.fcn(10)
[1] 0.9995235
> my.fcn(-5)
[1] 1.012727
> my.fcn(5)
2006 Apr 04
2
Return function from function with minimal environment
Hi,
this relates to the question "How to set a former environment?" asked
yesterday. What is the best way to to return a function with a
minimal environment from a function? Here is a dummy example:
foo <- function(huge) {
scale <- mean(huge)
function(x) { scale * x }
}
fcn <- foo(1:10e5)
The problem with this approach is that the environment of 'fcn' does
not
2010 Jun 09
2
OOP and passing by value
Greetings,
I love the R system and am sincerely grateful for the great effort the
product and contributors
are delivering.
My question is as follows:
I am trying to use S4 style classes but cannot write functions that modify
an object
because paramter passing is by value.
For example I want to do this:
setGeneric("setData", function(this,fcn,k){ standardGeneric("setData")
2016 Mar 13
1
formals(x)<- drops attributes including class
Just checking in to see whether it is intended or not that assigning
new formals to a function/closure causes any attributes to be dropped:
EXAMPLE:
> fcn <- structure(function() {}, foo="foo", class=c("foo"))
> str(fcn)
function ()
- attr(*, "srcref")=Class 'srcref' atomic [1:8] 1 18 1 30 18 30 1 1
.. ..- attr(*, "srcfile")=Classes
2011 Mar 15
1
Problem with nls.lm function of minpack.lm package.
Dear R useRs,
I have a problem with nls.lm function of minpackl.lm package.
I need to fit the Van Genuchten Model to a set of data of Theta and hydraulic conductivity with nls.lm function of minpack.lm package.
For the first fit, the parameter estimates keep changing even after 1000 iterations (Th)
and
I have a following error message for fit of hydraulic conductivity (k);
Reason for
2008 Apr 22
4
how to convert non numeric data into numeric?
I am having the following error in my function
function(theta,reqdIRR)
{
theta1<-theta[1]
theta2<-theta[2]
n<-length(reqdIRR)
constant<- n*(theta1+theta2)
sum1<-lapply(reqdIRR*exp(theta1),FUN = sum)
sum2<-lapply(exp(theta2 - reqdIRR*exp(theta1)),FUN = sum)
sum = sum1 + sum2
log.fcn = constant - as.numeric(sum)
result = - log.fcn
return(result)
}
*error :
2003 Oct 30
2
'nls' and its arguments
Dear R experts!
I'd to fit data by 'nls' with me-supplied function 'fcn'.
1) I'd like 'fcn' to accept arbitrary arguments, i.e. I defined it
as f(...) {<body>}. (Ok, that's not actually impotant).
2) Second, I would NOT like to supply every parameter in the formula.
To illustrate this, let's look at the last example of 'nls' help
2012 Jan 25
1
solving nls
Hi,
I have some data I want to fit with a non-linear function using nls, but it
won't solve.
> regresjon<-nls(lcfu~lN0+log10(1-(1-10^(k*t))^m), data=cfu_data,
> start=(list(lN0 = 7.6, k = -0.08, m = 2)))
Error in nls(lcfu ~ lN0 + log10(1 - (1 - 10^(k * t))^m), data = cfu_data, :
step factor 0.000488281 reduced below 'minFactor' of 0.000976562
Tried to increase minFactor
2010 May 14
1
debugging substitute function in R 2.11
Dear list,
A while ago I found in the web a function (sadly I can't credit the
author as I don't remember where I picked it up from) whose output is
a dataframe that lists for every object in the global environment its
class, mode, dim & length (where applicable). It is meant to be an
upgrade to the ls () function with a far more informative and detailed
output and it quickly became
2007 Sep 29
2
Help with functions (iterations)
Hello:
I am a bit confused by this problem. Can anyone give me some advice on this I would greatly appreciate it. Thank you for all your help.
Need to create a for loop that saves the estimate of pi from each 0f 100 separate iterations and store it in a numeric vector (of length 100). The for loop should be placed in a function that allows the user to vary the sample size, the simulation size,
2024 Feb 06
1
[EXTERNAL] Re: NOTE: multiple local function definitions for ?fun? with different formal arguments
Here's a dummy example that I think illustrates the problem:
toto <- function() {
if (runif(1) < 0.5)
function(a) a
else
function(a,b) a+b
}
> fcn <- toto()
> fcn(1,2)
[1] 3
> fcn <- toto()
> fcn(1,2)
[1] 3
> fcn <- toto()
> fcn(1,2)
Error in fcn(1, 2) : unused argument (2)
How can you use the returned function, if you get different arguments?
2005 Oct 09
3
[ subscripting sometimes loses names (PR#8192)
--rwEMma7ioTxnRzrJ
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
R, like recent versions of S-Plus, sometimes - but not always - loses
names when subscripting objects with "[". (Earlier versions of S and
S-Plus had the correct, name-preserving behavior.) This seems bad, it
would be better to remove names only by explicit request, not as an
accidental
2005 Sep 07
1
Tracebacks with tryCatch() and withCallingHandlers()?
When batch processing analysis, I use tryCatch() for failure handling
and to prevent unwanted interrupts. I write detailed progress to log
file and conditions (warnings and errors) are written to the same log
file immediately by using withCallingHandlers(..., condition=function(c)
cat(c, file=logFile)). However, I would also like to write the call
stack to the log file to further simplify
2001 Jul 03
1
extracting pkg fcn info
I would like to learn what functions are available in a given R package
(say, the pkg sma) and
what the arguments and return value is for each of those functions.
Is there an R CMD available for that or a suggested method for easily
extracting
that information?
thanks,
Natalie.
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read
2011 Dec 14
1
uniroot function question
I have one equation, two unknowns, so I am trying to build the solution set
by running through possible values for one unknown, and then using uniroot
to solve for the accompanying second solution, then graphing the two
vectors.
p0 = .36
f = function(x) 0.29 * exp(5.66*(x - p0))
f.integral = integrate(f, p0, 1)
p1 = p0 + .01
i = 1
n = (1 - p0)/.01
p1.vector = rep(0,n)
p2.vector = rep(0,n)
for (i
2002 Jan 23
1
Question about substitute (and eval, parse and deparse)
I would like to define a function at run-time, but I just can't make it
work (please don't question why I want to do this, because that would be
too much to explain). For example, I want to define the the following
function
foo <- function(x) {
cat("Function", "foo", "was called with argument", x, ".\n")
}
However, I would like to
2006 Apr 05
1
predict.smooth.spline.fit and Recall() (Was: Re: Return function from function and Recall())
Hi,
forget about the below details. It is not related to the fact that
the function is returned from a function. Sorry about that. I've
been troubleshooting soo much I've been shoting over the target. Here
is a much smaller reproducible example:
x <- 1:10
y <- 1:10 + rnorm(length(x))
sp <- smooth.spline(x=x, y=y)
ypred <- predict(sp$fit, x)
# [1] 2.325181 2.756166 ...
2008 Aug 26
2
Problem with Integrate for NEF-HS distribution
I need to calcuate the cumulative probability for the Natural Exponential Family - Hyperbolic secant distribution with a parameter theta between -pi/2 and pi/2. The integration should be between 0 and 1 as it is a probability.
The function "integrate" works fine when the absolute value of theta is not too large. That is, the NEF-HS distribution is not too skewed. However, once the
2024 Feb 06
1
[EXTERNAL] Re: NOTE: multiple local function definitions for ?fun? with different formal arguments
Because functions get called and therefore, the calling sequence matters. It?s just protecting you from yourself, but as someone pointed out, there?s a way to silence such notes.
G
From: Herv? Pag?s <hpages.on.github at gmail.com>
Sent: Tuesday, February 6, 2024 2:40 PM
To: Izmirlian, Grant (NIH/NCI) [E] <izmirlig at mail.nih.gov>; Duncan Murdoch <murdoch.duncan at gmail.com>;
2008 Jan 08
1
use "save.image" inside a function?
Hi, All
I'm trying to save the workspace as a Check Point for
possible future come back. The "save.image" function
works if the whole coding is in a main function:
rm(list=ls())
t = 1
t = t+1
print(paste("before",t))
# [1] "before 2"
save.image("tt.RData")
t = t+1
print(paste("after",t))
# [1] "after 3"