problems with $ using snowFall

95 views Asked by At

I am trying to use snowFall to speed up my code using a cluster. I simplified version of my code would be

library(snowfall)
pbsnodefile = Sys.getenv("PBS_NODEFILE")
machines <- scan(pbsnodefile, what="")
machines
nmach = length(machines)
nmach

sfInit(parallel=TRUE,type='SOCK',cpus=nmach,socketHosts=machines)

examp <- function(W,Y){       
    guess=lm(Y~W)
    return(guess$coef)
}

makedat <- function(N){
###Generating a dataset.
#Covariate vector
W <- mvrnorm(N,mu = rep(0,2),Sigma = matrix(c(1,0.8,0.8,1),nrow = 2))
Y <- rnorm(N)  
result <- data.frame(W = W,Y= Y)
return(result)
}

sfExport("examp")                                                                                                                    
sfExport("makedat") 
sfLibrary(MASS)


wrapper <- function(sim){
data <- makedat(100)
result <- examp(W = cbind(data[,1],data[,2]),Y = data[,3])
return(result)
}

nSim <- 2

result = sfLapply(1:nSim,wrapper)
save(result)

sfStop()

The aim of this was to only output the coefficient of the lm object (guess$coef), but the output that I am getting is the whole lm object. So it seems to me that the $ is not working. Later in my code (not included here I am experiencing the same problem, that is the $ does not seem to be working). All suggestions are greatly appreciated.

1

There are 1 answers

2
bkielstr On

If you are just trying to get the coefficient out of the model, you could use:

coef(guess)[2] 

which would retrieve the 2nd coefficient, the slope.

A followup comment (I'm new here, so wasn't sure if I actually add new information to a separate but sort of related question here, so tell me if I did this wrong!) to the OP:

I find it easier to first make something an object to understand its structure. Following the example found in ?nlm():

f <- function(x) sum((x-1:length(x))^2)
nlm(f, c(10,10))
nlm(f, c(10,10), print.level = 2)
utils::str(nlm(f, c(5), hessian = TRUE))

f <- function(x, a) sum((x-a)^2)
nlm(f, c(10,10), a = c(3,5))
f <- function(x, a)
{
res <- sum((x-a)^2)
attr(res, "gradient") <- 2*(x-a)
res
}
nlm(f, c(10,10), a = c(3,5))

###HOW TO ACCESS THE ESTIMATE###
#making the nlm() output an object:
nlm.obj<- nlm(f, c(10,10), a = c(3,5))
#checking the structure
str(nlm.obj) #a list
nlm.obj[[2]]  #accessing estimate
nlm.obj[[2]][1] #accessing the first value within the estimate

Please, anyone, comment on the quality of this answer so I can help in a better way in the future.