[R] Random variable input in mlm

Andris Jankevics andza at osi.lv
Fri Mar 31 12:35:17 CEST 2006


Hello useRs, 

I have a Liquid Chromatography data of mixtures. I have a 13 variables which 
are integrals of peaks, and 40 observations as chromatography samples, and 
concentrations of subtances as response variablse Y. 
I am trying to bild the best regression model with MLM and PLS for these data, 
and athe beginning I want to find out which variables are impotant for model.
For this  reason I wrote this simple function which  start with 2 variables 
and do +1 in each step in data frame and calculate ar SSE of predicton on 
Validation data at each model.

Also I wrote these functions to select variables:

D <- Z[length(Z):(1+i) , 
F3 <- function (i) 
	{ center <- round(length(Z)/2)
	D <- Z[center:(length(Z)-i)] }
F4 <- function (i) 
	{ center <- round(length(Z)/2)
	D <- Z[center:(i)] }
F5 <- function (i)
	{ center <- round(length(Z)/2)
	D <- Z[(center+i):(center-i)] }

Is there any function in R to randomize a variable selection from data?
My be I should optimeze something in my code below?

set.seed (1)
X1 <- rnorm(10,sd=0.1)
X2 <- rnorm(10,sd=1)
X3 <- rnorm(10,sd=0.1)
X4 <- rnorm(10,sd=0.1)
X5 <- rnorm(10,sd=0.1)
Y1 <- seq(0,1.8,0.2)
Y2 <- seq(1,1.9,0.1)
Z <- data.frame (X1,X2,X3,X4,X5)

Y1v <- (1:10)
Y2v <- (10:1)
X1v <- rnorm(10,sd=0.1)
X2v <- rnorm(10,sd=1)
X3v <- rnorm(10,sd=0.1)
X4v <- rnorm(10,sd=0.1)
X5v <- rnorm(10,sd=0.1)
Zv <- data.frame (X1v,X2v,X3v,X4v,X5v)

Fl <- function (i)  {D <- Z[,1:(1+i)]
	CAL <- data.frame (D,Y1,Y2)
	MLM <- lm(cbind(Y1,Y2)~., data = CAL)		
	V <- Zv[,1:(1+i)]
	VAL <- data.frame(V,Y1v,Y2v)	
	PRED <- predict (MLM,VAL)
	Y2x <- Y2 - PRED[,2]
	Y1x <- Y1 - PRED[,1]
	SSEY1 <- sum(Y1x[1:length(V[,2])]^2)
	SSEY2 <- sum(Y2x[1:length(V[,2])]^2)
	RES <- data.frame (Vars = structure(dimnames(as.matrix(D))[2],dim =1),SSEY1 = 
SSEY1, SSEY2 = SSEY2)
	print (RES) }

lapply (1:(length(Z)-1),Fl)


Andris Jankevics




More information about the R-help mailing list