Can you please explain to me why the code complains saying that Samdat is not found?
I am trying to switch between models, so I declared a function that contains these specific models and I just need to call this function as one of the argument in the get.f function where the resampling will change the structure for each design matrix in the model. The code complains that Samdat is not found when it is found.
Also, is there a way I can make the conditional statement if(Model == M1()) instead of having to create another argument M to set if(M==1)?
Here is my code:
dat <- cbind(Y=rnorm(20),rnorm(20),runif(20),rexp(20),rnorm(20),runif(20), rexp(20),rnorm(20),runif(20),rexp(20))
nam <- paste("v",1:9,sep="")
colnames(dat) <- c("Y",nam)
M1 <- function(){
a1 = cbind(Samdat[,c(2:5,7,9)])
b1 = cbind(Samdat[,c(2:4,6,8,7)])
c1 = b1+a1
list(a1=a1,b1=b1,c1=c1)}
M2 <- function(){
a1= cbind(Samdat[,c(2:5,7,9)])+2
b1= cbind(Samdat[,c(2:4,6,8,7)])+2
c1 = a1+b1
list(a1=a1,b1=b1,c1=c1)}
M3 <- function(){
a1= cbind(Samdat[,c(2:5,7,9)])+8
b1= cbind(Samdat[,c(2:4,6,8,7)])+8
c1 = a1+b1
list(a1=a1,b1=b1,c1=c1)}
#################################################################
get.f <- function(asim,Model,M){
sse <-c()
for(i in 1:asim){
set.seed(i)
Samdat <- dat[sample(1:nrow(dat),nrow(dat),replace=T),]
Y <- Samdat[,1]
if(M==1){
a2 <- Model$a1
b2 <- Model$b1
c2 <- Model$c1
s<- a2+b2+c2
fit <- lm(Y~s)
cof <- sum(summary(fit)$coef[,1])
coff <-Model$cof
sse <-c(sse,coff)
}
else if(M==2){
a2 <- Model$a1
b2 <- Model$b1
c2 <- Model$c1
s<- c2+12
fit <- lm(Y~s)
cof <- sum(summary(fit)$coef[,1])
coff <-Model$cof
sse <-c(sse,coff)
}
else {
a2 <- Model$a1
b2 <- Model$b1
c2 <- Model$c1
s<- c2+a2
fit <- lm(Y~s)
cof <- sum(summary(fit)$coef[,1])
coff <- Model$cof
sse <-c(sse,coff)
}
}
return(sse)
}
get.f(10,Model=M1(),M=1)
get.f(10,Model=M2(),M=2)
get.f(10,Model=M3(),M=3)