Dear R community members and R experts 

 

I am stuck at a point and I tried with my colleagues and did not get it out.
Sorry, I need your help. 

 

Here my data (just created to show the example):

 

# generating a dataset just to show how my dataset look like, here I have x
variables

# x1 .........to X1000 plus ind and y 

ind <- c(1:100)

y <- rnorm(100, 10,2)

set.seed(201)

P <- vector()

dataf1 <- as.data.frame(matrix(rep(NA, 100000), nrow=100))

dataf <- data.frame (dataf1, ind,y)

names(dataf) <- (c(paste("x",1:1000, sep=""),"ind", "y"))

for(i in 1:1000) {

dataf[,i] <- rnorm(100)

}

 

# my intension was to fit a model that would fit the following fashion:

y ~ x1 +x2, y ~ x3+x4, y ~ x5+ x6............y ~ x999+x1000 (to end of the
dataframe)

 

# please not that I want to avoid to fit  y ~ x2 + x3 or  y ~ x4 + x5 (means
that I am selecting two x variables at time to end)

# question: how can I do this and put inside a user function as I worked out
the following??? 

 

 

# defining function for lm model 

mylm <- function (mydata,nvar) {

    y <- NULL

    P1 <- vector (mode="numeric", length = nvar)

    P2 <- vector (mode="numeric", length = nvar)

    for(i in 1: nvar) {    

    print(P1[i] <- summary(lm(mydata$y ~   mydata[,i]) +
mydata[,i+1]$coefficients[2,4]))

    print(P2[i] <- summary(lm(mydata$y ~   mydata[,i]) +
mydata[,i+1]$coefficients[2,5]))

    print(plot(nvar, P1))

    print(plot(nvar, P2))

        }

} 

 

# applying the function to mydata 

mylm (dataf, 1000)

 

Does not work?? The following is the error message: 

Error in model.frame.default(formula = mydata$y ~ mydata[, i],
drop.unused.levels = TRUE) : 

  invalid type (NULL) for variable 'mydata$y'

 

Please help !

 

Thanks;

 

Umesh R


	[[alternative HTML version deleted]]

