[R] fit a nonlinear model using nlm()
William Simpson
william.a.simpson at gmail.com
Tue Jul 17 12:58:13 CEST 2007
I am trying to fit a nonlinear model using nlm().
My application of nlm() is a bit complicated.
Here is the story behind the model being fit:
The observer is trying to detect a signal corrupted by noise.
On each trial, the observer gets stim=signal+rnorm().
In the simulation below I have 500 trials. Each row of stim is a new trial.
On each trial, if the cross-correlation between the stim and
the signal is above some criterion level (crit=.5 here), the
observer says "signal" (resp=1), else he says "no signal"
(resp=0).
So the situation is this: I know resp and stim for each trial.
I would like to estimate crit and signal from these.
(You might say that I already know the signal and crit. But the
idea here is that the observer cross-correlates the stim with an
internal template that will not be identical to the actual signal.
I want to estimate this template. Also the observer's crit may
differ from the "correct" one.)
In the code below, please help me get the f() and nlm() bits right.
I want to estimate signal and crit given stim and resp.
Thanks very much for any help!
Bill
x<-1:100
con<-.1
signal<-con*cos(2*pi*3*x/length(x))
crit<-.5
noisesd<-.1
# each row is a new stim (trial). 500 trials
resp<-array(dim=500)
stim<-matrix(nrow=500,ncol=100)
for (i in 1:500)
{
stim[i,]<-signal+rnorm(n=length(signal), mean=0, sd=noisesd)
if (sum(stim[i,]*signal)>crit) (resp[i]<-1) else (resp[i]<-0)
}
f<-function(signalest)
{
r<-array(dim=500)
for (i in 1:500)
{
r[i]<-sum(stim[i,]*signalest)>critest
}
sum((r-resp)^2)
}
fit<-nlm(f, stim[1,])
More information about the R-help
mailing list