Displaying 20 results from an estimated 20000 matches similar to: "A function as argument of another function"
2003 Dec 16
3
`bivariate apply'
dear all,
Given a matrix A, say, I would like to apply a bivariate function to each
combination of its colums. That is if
myfun<-function(x,y)cor(x,y) #computes simple correlation of two vectors x
and y
then the results should be something similar to cor(A).
I tried with mapply, outer,...but without success
Can anybody help me?
many thanks in advance,
vito
2007 Dec 06
1
differences in using source() or console
Dear all,
Is there *any* reason explaining what I describe below?
I have the following line
myfun(x)
If I type them directly in R (or copy/past), it works..
However if I type in R 2.6.1
> source("code.R") ##code.R includes the above line
Error in inherits(x, "data.frame") : object "d" not found
namely myfun() does not work correctly.
In particular the
2002 Sep 18
3
problem in deparse(substitute())
Hi all,
I am experiencing the following quite strange (at least in my knowledge)
problem in a simple function like the following:
fn<-function(y,v=2){
n<-length(y)
y<-y[(v+1):(n-v)]
plot(y,type="l",lty=3,xlab="Time",ylab=deparse(substitute(y)))
}
fn(rnorm(50)) #look at the plot!!!
The plot appears with numbers on the left side!
If I delete the
2002 Apr 30
1
MemoryProblem in R-1.4.1
Hi all,
In a simulation context, I'm applying some my function, "myfun" say, to a
list of glm obj, "list.glm":
>length(list.glm) #number of samples simulated
[1] 1000
>class(list.glm[[324]]) #any component of the list
[1] "glm" "lm"
>length(list.glm[[290]]$y) #sample size
[1] 1000
Because length(list.glm) and the sample size are rather large,
2001 Dec 03
1
fitting models with the subset argument
Hi all,
I'd like to fit model where the terms both are in the data.frame, mydata
say, and are vectors *not in the data.frame*.
>obj<-glm(y~x, data=mydata) #works
>Z<-pmax(mydata$x-20,0)
>(length(Z)==length(obj$y))
>[1] TRUE
>update(obj,.~.+Z) #works
However for some subset it doesn't works:
>obj<-glm(y~x, data=mydata, subset=f==1) #works
2004 Oct 26
2
vcov method for 'coxph' objects
Dear all,
The help file for the generic function vcov states
"Classes with methods for this function include: 'lm', 'glm', 'nls', 'lme',
'gls', 'coxph' and 'survreg' (the last two in package 'survival')."
Since, I am not able to use vcov.coxph(), I am wondering whether I am
missing something (as I suspect..)
regards,
vito
2003 Mar 12
1
simulating 'non-standard' survival data
Dear all,
I'm looking for someone that help me to write an R function to simulate
survival data under complex situations, namely time-varying hazard ratio,
marginal distribution of survival times and covariates. The algorithm is
described in the reference below and it should be not very difficult to
implement it. However I tried but without success....;-(
Below there the code that I used; it
2018 Jan 30
2
variable names in lm formula ~.
dear all,
Is the following intentional? Am I missing anything in documentation?
d<-data.frame(y=rnorm(10,5,.5),exp=rnorm(10), age=rnorm(10))
formula(lm(exp(y)~exp+age, data=d))
#--> exp(y) ~ exp + age
formula(lm(exp(y)~., data=d))
#--> exp(y) ~ age
variable 'exp' (maybe indicating "experience") is not included in the
model. The same happens with 'log' (and
2008 Jun 30
2
difference between MASS::polr() and Design::lrm()
Dear all,
It appears that MASS::polr() and Design::lrm() return the same point
estimates but different st.errs when fitting proportional odds models,
grade<-c(4,4,2,4,3,2,3,1,3,3,2,2,3,3,2,4,2,4,5,2,1,4,1,2,5,3,4,2,2,1)
score<-c(525,533,545,582,581,576,572,609,559,543,576,525,574,582,574,471,595,
557,557,584,599,517,649,584,463,591,488,563,553,549)
library(MASS)
library(Design)
2017 Jun 18
2
R_using non linear regression with constraints
I am using nlsLM {minpack.lm} to find the values of parameters a and b of
function myfun which give the best fit for the data set, mydata.
mydata=data.frame(x=c(0,5,9,13,17,20),y = c(0,11,20,29,38,45))
myfun=function(a,b,r,t){
prd=a*b*(1-exp(-b*r*t))
return(prd)}
and using nlsLM
myfit=nlsLM(y~myfun(a,b,r=2,t=x),data=mydata,start=list(a=2000,b=0.05),
lower = c(1000,0),
2017 Jun 18
3
R_using non linear regression with constraints
I am not as expert as John, but I thought it worth pointing out that the
variable substitution technique gives up one set of constraints for
another (b=0 in this case). I also find that plots help me see what is
going on, so here is my reproducible example (note inclusion of library
calls for completeness). Note that NONE of the optimizers mentioned so far
appear to be finding the true best
2017 Jun 18
0
R_using non linear regression with constraints
I ran the following script. I satisfied the constraint by
making a*b a single parameter, which isn't always possible.
I also ran nlxb() from nlsr package, and this gives singular
values of the Jacobian. In the unconstrained case, the svs are
pretty awful, and I wouldn't trust the results as a model, though
the minimum is probably OK. The constrained result has a much
larger sum of squares.
2018 Jan 30
0
variable names in lm formula ~.
Functions are first class objects, so some kind of collision is bound to happen if you do this... so don't.
--
Sent from my phone. Please excuse my brevity.
On January 30, 2018 3:11:56 AM PST, "Vito M. R. Muggeo" <vito.muggeo at unipa.it> wrote:
>dear all,
>Is the following intentional? Am I missing anything in documentation?
>
2007 Jul 30
1
stop criteria when "L-BFGS-B needs finite values of 'fn' " in optim
Hi all!
I'm running some simulations and I need to estimate some paramaters with
optim( ),
in some cases optim stops with the next message:
"L-BFGS-B needs finite values of 'fn' "
I would like to know how to include and "if" condition when this happen,
could it be something like:
myfun <- optim(....) # run my function
2017 Jun 18
3
R_using non linear regression with constraints
https://cran.r-project.org/web/views/Optimization.html
(Cran's optimization task view -- as always, you should search before posting)
In general, nonlinear optimization with nonlinear constraints is hard,
and the strategy used here (multiplying by a*b < 1000) may not work --
it introduces a discontinuity into the objective function, so
gradient based methods may in particular be
2018 Jan 30
1
variable names in lm formula ~.
Well...
?terms.formula says:
"data: a data frame from which the meaning of the special symbol . can
be inferred. It is unused if there is no . in the formula."
So this seems to me to be an obscure bug, as I have found no warning
against this admittedly confusing but still, I think, legal syntax.
Note:
> d <- data.frame(log = runif(10), x = 1:10)
> y <- rnorm(10,5)
>
2017 Jun 18
0
R_using non linear regression with constraints
I've seen a number of problems like this over the years. The fact that the singular values of the Jacobian have
a ration larger than the usual convergence tolerances can mean the codes stop well before the best fit. That is
the "numerical analyst" view. David and Jeff have given geometric and statistical arguments. All views are useful,
but it takes some time to sort them all out and
2003 Feb 28
2
optim
Dear all,
I have a function MYFUN which depends on 3 positive parameters TETA[1],
TETA[2], and TETA[3]; x belongs to [0,1].
I integrate the function over [0,0.1], [0.1,0.2] and
[0.2,0.3] and want to choose the three parameters so that
these three integrals are as close to, resp., 2300, 4600 and 5800 as
possible. As I have three equations with three unknowns, I expect the
exact fit, i.e., the SS
2004 Nov 02
3
time dependency of Cox regression
Hi,
How can I specify a Cox proportional hazards model
with a covariate which i believe its strength on
survival changes/diminishes with time? The value of
the covariate was only recorded once at the beginning
of the study for each individual (e.g. at the
diagnosis of the disease), so I do not have the time
course data of the covariate for any given individual.
For example, I want to state at the
2002 Jan 04
1
taking variables from data.frame
Dear all,
Two of the arguments of my function fn(), say, are a glm-obj and a variable
in the data.frame where there are the variable of the obj too.
Is there a way to build a function that takes the variables form the same
data.frame? For instance, the update() function does so:
>obj<-glm(....,data=mydata)
>update(obj,.~.+x) #works becuse it searches x in mydata
But for my function: