0

I am new in using R and I am not familiar with the writing R loops. I need you to help me out of the following R programing. The objectives of the R code is to get the AIC for all the model with different df.

The R code that I have written were:

library(splines)
library(dlnm)

For (i in 1:30)
    argvar1 <-list(type="bs", df=2[i],cen=50)
    arglag1<- list(type="ns",df=3)
    cb1 <-crossbasis(AFH6w,lag=24,argvar=argvar1,arglag=arglag1)
    argvar2 <-list(type="ns", df=11, cen=-2)
    arglag2 <- list(type="ns",df=3)
    cb2 <-crossbasis(OutTw,lag=24,argvar=argvar2,arglag=arglag2)
    model1 <-lm(NH3cH6w~ cb1 +cb2+DenH6w+as.factor(Month))
    AIC[i]=AIC(model1)
}

I think there are problems with the last line but I cannot figure it out after exhausted struggling.

I will really appreciate your help!

Shule

zero323
  • 322,348
  • 103
  • 959
  • 935
user26221
  • 33
  • 5
  • 1
    The way your code is written, the only thing in the `for` loop is the first line: `argvar1 <-list(type="bs", df=2[i],cen=50)`, and everything else is outside the loop. If you want *all* these lines inside the loop, you should enclose them in curly braces (it seems to me that's exactly what you tried to do... but you forgot the opening curly brace `{` after `for(i in 1:30)`... and didn't forgot the closing one). – Barranka Sep 30 '13 at 20:32
  • 1
    Use readable variable names! They improve code readability tremendously! – Konrad Rudolph Sep 30 '13 at 20:42

1 Answers1

1

You are missing opening curly brackets and for should be lowercase:

for (i in 1:30) {
        argvar1 <-list(type="bs", df=2[i],cen=50)
        arglag1<- list(type="ns",df=3)
        ...
}

Other than that it is hard to say. Please read How to make a great R reproducible example? before posting.

Community
  • 1
  • 1
zero323
  • 322,348
  • 103
  • 959
  • 935