For Loop with MCMCglmm Regression - r

I've looked at some of the answers for this question already, there were only two I found helpful and I still cannot get my loop to execute. I am struggling to use a fixed formula for the MCMCglmm package. I have a lot of models to test with this package, and I would like to make a loop to make the work easier. Each time I run MCMCglmm my intention is to do so with a "fixed" formula, and through each iteration of the loop I want to change one of the variables and input a modified version of the "fixed" formula. Here is my code thus far:
for (i in 5:10){
fixed <- as.formula(paste(as$area_pva ~ as$apva_1yr + as$year + as.numeric(unlist(as[i]))))
print(fixed)
model <- MCMCglmm(fixed=fixed,
rcov=~units, family="gaussian",
data=as,start=NULL, prior=NULL, random=NULL, tune=NULL,
pedigree=NULL, nodes=NULL, scale=FALSE, nitt=30000,
thin=30, burnin=1000, pr=TRUE, pl=TRUE, verbose=TRUE,
DIC=TRUE, singular.ok=FALSE, saveX=TRUE, saveZ=TRUE,
saveXL=TRUE, slice=FALSE, ginverse=NULL)
summary(model)
}
Please, if you can help me make this loop execute properly I would appreciate it.

Never mind, I've got the answer. I needed to make the whole formula a series of strings, like this:
fixed <- as.formula(paste("as$area_pva~as$apva_1yr+as$year+", colnames(as)[i], sep=""))
It works perfectly now.

Related

R lm() weights argument being ignored when placed inside function

I am trying to figure out why the following piece of code ignores the weights argument and produces simply an unweighted regression analysis. If I remove the function wrapping everything works fine. The only way the code runs is if I change the code so that weights=richiu, but I really don't know why the original code would not work in a function environment. Any clear explanations would be really helpful.
set.seed(1)
UU=rnorm(1000)
ZZ=rnorm(UU)
futa=data.frame(UU,ZZ)
stringy=paste("ZZ~UU",sep="")
friga=function(stringy,futa){
richiu=rnorm(1000)
futa$richiu=richiu
print(colnames(futa))
lm(stringy,data=futa, weights=futa$richiu)
}
friga(stringy,futa=futa)
lm(ZZ~UU)

Dynamic hedge ratio in R

I was trying to convert the Chan code about Pairs Trading strategy with dynamic hedge ratio into R.
I have already done all work with a steady hedge, but if I want to replicate his "for" loop I'm in trouble. Here my code for that part
lookback=20
hedge=rep(NaN,length(stockY))
for (i in lookback:length(hedge)){
reg=lm(stockY[i-lookback +1:i]~stockX[i-lookback +1:i])
hedge[i]=reg[1]$coeff[2]
}
I tried many different attempts but my low level in R is pretty evident here. I'm not trying to use a "lapply" function but just a for loop. Hope someone can help me. Thanks
Ok It seems I did it. The following is my code:
lookback=20
hedge=data.frame(hedge=rep(0,length(stockY)))
for (i in lookback:length(stockY)){
reg=summary(lm(stockY[i-lookback +1:i]~stockX[i-lookback +1:i]))
hedge[i,1]=reg$coefficients[2,1]
}
So, now I'd know how to extract my residuals. I mean, I need to list all residuals from regression. Unfortunately if I write reg$residuals it returns my last 20 residuals from the last iteration of the loop. So, I tried to include another "res" vector like "hedge" but.....I can't extract my residuals. Please, can someone help me?

anova.cca cannot find object within user defined function

I want to execute the same rda analysis sequence (fitting a model, testing significance of the model, the axis, and the term, plotting the data) on subsets of the same datasets. Therefore I wrote a function. The problem now is that the call to the anova.cca function does not work well within a function when I want to test the axis. It cannot find the Y.sub dataset
Error in eval(expr, envir, enclos) : object 'RV.sub' not found
Minimal working example:
library(vegan)
data(dune)
data(dune.env)
rda.subsetfunc <- function(RV, Y){
#RV.sub <- subset(RV, !Y$Use%in%c("BF"))
#Y.sub <- subset(Y, !Y$Use%in%c("BF"))
RV.sub <- RV; Y.sub <- Y
rda.mod <- rda(RV.sub ~ Manure, Y.sub)
axis.test <- anova(rda.mod, by = "axis")
return(list(rda.mod, axis.test))
}
rda.subsetfunc(RV = dune, Y = dune.env)
I found some other related questions like here but that seems be a lot more complicated than what I am doing. I tried to implement the do.call approach as is mentioned here but I couldn't get it to work. If it is really not possible to do this without really digging deep into functions I will find a way of programming around it. But to me, it feels like I'm trying to do something that makes total sense. So it is probably more likely that I am doing something wrong than that I am doing something impossible.
This a scoping issue in anova.cca(..., by="axis") which should find items from several different environments when it is updating the formula (I won't go into technical details). You really cannot embed the function to analyse the significances of axes. This is a known issue. We have solved that in the development version of vegan. The re-designed function in https://github.com/vegandevs/vegan seemed to work with this example. All ordination and significance functions are radically changed there, and they are not yet completely finished. We plan to release them in vegan 2.5-0 in the last quarter of 2017, but they are not finished yet.
The problem is that anova.cca(..., by = "axis") must find items that it builds within the function, and in addition it can find items that were available when the original model was built, but it cannot find items that you generate in functions that embed the function. You must circumvent this by making your embedding function to write its objects somewhere that these can be found. The easiest (but dirty) solution is to write them into parent environment with <<-. The following version of your function adds this <<- and seems to work in vegan 2.4-3
rda.subsetfunc <- function(RV, Y){
RV.sub <<- RV; Y.sub <- Y
rda.mod <- rda(RV.sub ~ Manure, Y.sub)
axis.test <- anova(rda.mod, by = "axis")
list(rda.mod, axis.test)
}
rda.subsetfunc(RV = dune, Y = dune.env)

iterating a coxph() model using various sets of covariates

I'm still a little new to R, so this may be a basic question.
I am looking for risk estimates for a joint-cox model using coxph(). I have to iterate the model for about 60 times using various combinations of variables. Since each iteration of the model will have different covariates (and main exposures), I want to write one function to do it. In the age-adjusted model I just had the main exposure, everything runs fine. I can add the covariates, it runs... I just need a way to write a single function where the "covars" can be whatever I put into the function call.
Note: this is a simplified version, it runs just fine, I just want to make it work without writing out 60 unique iterations of it.
subtype <- function(expo, covars){
temp <- coxph(Surv(FAIL, OUTCOME) ~ joint[[expo]]*strata(EVENT2)+
covars+
cluster(ID)+strata(AGE_INT),
na.action=na.exclude,
data=joint)
return(summary(temp))
}
results <- subtype("RACE", covars=...)
results2 <- subtype("GENDER", covers=...
When I did this macro programing in SAS, it was easy.
Thank you for your help.

Trying to use the baseline function with apply instead of a for loop in R

I have spectral data that I am trying to run PCA on. To learn how to do this I have created a matrix with two distinct groups and then pulled a file in that has typical wavelengths. I am pre-processing the data and have run into issues with baseline correction. I can do it with a for loop, but want to know if I can do it with apply instead. I get different errors depending on what I am trying and don't know if it is even possible. My most recent error is:
Error in matrix(0, np[1], np[2]) : non-numeric matrix extent.
I get this immediately after:
playdata.baseline<-apply(playdata,1, baseline,lambda=1,hwi=20,it=30,int=800,method='fillPeaks')
Can I use apply for the baseline function? If yes, why is it not working with the rest of the code that works fine using a for loop for baseline?
Here is what works:
#baseline corrections
playdata.baseline=matrix(0,ncol=nrow(playdata),nrow=ncol(playdata))
playdata.bc=c()
playdata=t(playdata)
for (n in 1:(length(playdata[,1]))){
playdata.bc=baseline(playdata[n,,drop=FALSE],
lambda=1, hwi=20, it=30,
int=800, method='fillPeaks')
playdata.baseline[n,]=playdata.bc#corrected
}
playdata=playdata.baseline
playdata=t(playdata)
Keeping everything the same until # baseline corrections, the apply attempt is as follows:
#baseline corrections
playdata=t(playdata)
playdata.baseline <- apply(playdata, 1, baseline, lambda=1, hwi=20,
it=30, int=800, method='fillPeaks')
playdata=t(playdata)

Resources