[R] nls: Fitting two models at once?

Martin Ballaschk qcumba at web.de
Thu Nov 6 22:22:12 CET 2008


Hello,

I'm still a newbie user and struggling to automate some analyses from
SigmaPlot using R. R is a great help for me so far!

But the following problem makes me go nuts.

I have two spectra, both have to be fitted to reference data. Problem: the
both spectra are connected in some way: the stoichiometry of coefficients
"cytf.v"/"cytb.v" is 1/2.
{{In the SigmaPlot workflow one has to copy the two spectra into one column
beneath each other and the  two spectra are somehow treated as one curve -
like in http://home.arcor.de/ballaschk/cytbf-help/sigmaplot%20formula.png}}

Can anybody help? :(

I tried to condense everything to the "minimum" R script below to give an
impression of what I'm talking about.

	Martin



########################################################################
# "Minimal" R script reading remote data for convenience

### READ IN DATA
# first spectrum
asfe <- read.table("http://home.arcor.de/ballaschk/cytbf-help/asfe.csv")[, 1];
# second spectrum
dias <- read.table("http://home.arcor.de/ballaschk/cytbf-help/dias.csv")[, 1];

# reference data for fit, wavelength = wl
ref <- read.table("http://home.arcor.de/ballaschk/cytbf-help/reference.csv",
sep="\t", dec=".", header=T);
attach(ref);

### FITTING, problem: 2*cytf.v == cytb.v

# fit first spectrum to two reference spectra
asfe.fit <-
nls(
	asfe ~ (cytf.v * 28) * CYTF + hp.v * B559HP,
	start = list(cytf.v = 0.00005, hp.v = 0.000003) # arbitrary
);

# fit second spectrum to three reference spectra
dias.fit <-
nls(
	dias ~ (cytb.v * 24.6 * 2) * CYTB6 + lp.v * B559LP + c550.v * C550,
	start = list(cytb.v = 1, lp.v = 1, c550.v = 1) # arbitrary
);


# draw stuff
plot(
	1, 2,
	type="n",
	xlim = c(540, 575),
	ylim=c(-0.002, 0.008),
);

# first spectrum and fit
lines(wl, asfe, type="b", pch=19); # solid circles
lines(wl, fitted(asfe.fit), col = "red");

# second spectrum and fit
lines(wl, dias, type="b");
lines(wl, fitted(dias.fit), col = "blue");



More information about the R-help mailing list