Hi,

 here is a source code that allows estimate interactions for Conjoint Analysis in R. Obtained matrix xInteractions can be applied as input in partutilities (part-worth utility estimation) or others that can be obtained from: http://keii.ae.jgora.pl/nauka/procedury.zip.

subsets <- function(n, r) {
  if(is.numeric(n) & length(n) == 1) v <- 1:n else {
    v <- n
    n <- length(v)
  }
    subs <- function(n, r, v)
      if(r <= 0) NULL else
      if(r >= n) matrix(v[1:n], nrow = 1) else
      rbind(cbind(v[1], subs(n - 1, r - 1, v[-1])),
      subs(n - 1, r , v[-1]))
      subs(n, r, v)
} 

# function subsets by:
# Bill Venables
# CSIRO Laboratories
# PO Box 120, Cleveland, 4163
# AUSTRALIA


interval<-function(x,begins){
 output<-length(begins)
 for(i in 1:(length(begins)-1)){
  if((begins[i]<=x) &&  (x<begins[i+1])){
   output<-i
  }
 }
 output
}

allowed <-function(indexes,begins){
 resul<-TRUE
 for(i in 1:(length(indexes)-1))
 for(j in (i+1):length(indexes)){
  if(interval(indexes[i],begins)==interval(indexes[j],begins))resul<-FALSE  
 }
 resul
}

x<-as.matrix(x)
numVariables<-ncol(x)
interLevels<-2

maxComb<-interLevels+1

levels<-c(2,1,1)


begins<-seq(along.with=levels)
begins[1]<-1
for(i in 2:length(levels)){
  begins[i]<-begins[i-1]+levels[i-1]
}

xInterakcje<-x

for(i in 1:interLevels){
  for(j in  1:nrow(subsets(numVariables,i+1))){
    indexes<-subsets(numVariables,i+1)[j,]
    print(paste(paste(indexes,collapse=","),allowed(indexes,begins)))
    if(allowed(indexes,begins)){
     output<-x[,indexes[1]]
     for(k in 2:(i+1)){
       output<-output*x[,indexes[k]]
     }
     xInterakcje<-cbind(xInterakcje,output)
 }
  }
}
nam<-paste("x",1:numVariables,sep="")
for(i in 1:interLevels){
  for(j in  1:nrow(subsets(numVariables,i+1))){
    indexes<-subsets(numVariables,i+1)[j,]
    if(allowed(indexes,begins)){
     nam<-c(nam,paste(paste("x",indexes,sep=""),collapse="_"))
 }
  }
}
colnames(xInterakcje)<-nam

print(xInterakcje)

x<-as.data.frame(xInterakcje)

Best Regards,
Marcin
	[[alternative HTML version deleted]]

