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.
If you are just trying to get the coefficient out of the model, you could use:
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():
Please, anyone, comment on the quality of this answer so I can help in a better way in the future.