Function lm(...) returns an object of class 'lm'. How do I create an array of such objects? I want to do the following:
my_lm_array <- rep(as.lm(NULL), 20)
#### next, populate this array by running lm() repeatedly:
for(i in 1:20) {
my_lm_array[i] <- lm(my_data$results ~ my_data[i,])
}
Obviously the line "my_lm <- rep(as.lm(NULL), 20)" does not work. I'm trying to create an array of objects of type 'lm'. How do I do that?
Not sure it will answer your question, but if what you want to do is run a series of lm from a variable against different columns of a data frame, you can do something like this :
data <- data.frame(result=rnorm(10), v1=rnorm(10), v2=rnorm(10))
my_lms <- lapply(data[,c("v1","v2")], function(v) {
lm(data$result ~ v)
})
Then, my_lms would be a list of elements of class lm.
Well, you can create an array of empty/meaningless lm objects as follows:
z <- NA
class(z) <- "lm"
lm_array <- replicate(20,z,simplify=FALSE)
but that's probably not the best way to solve the problem. You could just create an empty list of the appropriate length (vector("list",20)) and fill in the elements as you go along: R is weakly enough typed that it won't mind you replacing NULL values with lm objects. More idiomatically, though, you can run lapply on your list of predictor names:
my_data <- data.frame(result=rnorm(10), v1=rnorm(10), v2=rnorm(10))
prednames <- setdiff(names(my_data),"result") ## extract predictor names
lapply(prednames,
function(n) lm(reformulate(n,response="result"),
data=my_data))
Or, if you don't feel like creating an anonymous function, you can first generate a list of formulae (using lapply) and then run lm on them:
formList <- lapply(prednames,reformulate,response="result") ## create formulae
lapply(formList,lm,data=my_data) ## run lm() on each formula in turn
will create the same list of lm objects as the first strategy above.
In general it is good practice to avoid using syntax such as my_data$result inside modeling formulae; instead, try to set things up so that all the variables in the model are drawn from inside the data object. That way methods like predict and update are more likely to work correctly ...
Related
Context: I'd like to save the results of a Likelihood ratio test for a multinomial logistic regression in several dynamic variables, but I'm not sure how I could do that. This is what I've been trying:
library(lmtest)
indels = c("C.T","A.G","G.A","G.C","T.C","C.A","G.T","A.C","C.G","A.del","TAT.del","TCTGGTTTT.del","TACATG.del","GATTTC.del")
my_list = list()
for (i in 1:length(indels)) {
assign(paste0("lrtest_results_",indels[i]), my_list[[i]]) = lrtest(multinom_model_completo, indels[i])
}
I was basically trying to save each variable (with the name lrtest_results_ + the dynamic part of the variable name which depends on the vector indels) in a list using the assign method and paste0, but it doesn't seem to be working. Any help is very welcome!
The best way is to lapply the test function to each element of the vector indels and assign the names after.
my_list <- lapply(indels, \(x) lrtest(multinom_model_completo, x))
names(my_list) <- paste0("lrtest_results_", indels)
I have defined two global variables as lists, let's say:
importance.5maturity <- list()
importance.10maturity <- list()
I also have a function which runs randomForest and I want to add the Importance of this function in the list, in each loop (I am using a rolling window). I believe this can be done using list.append().
The input of this function has a variable named maturity. I want to have an if statement, in a way that if the list name in global variable has the same number as in maturity, the function stores the Importance information in that particular list. For example,
b <- randomForest(y ~., data= d.na, mtry=5, ntree=1000, importance=TRUE)
if(maturity==5){
importance.5maturity <- list.append(Importance(b))
But I don't know how to match the maturity and the number (5 and 10) in the list name so the function would choose the correct list to store the information in automatically.
I also don't want to use local variables, which the function would return, since I am returning another data frame from it.
I have a list of data frames. I want to use lapply on a specific column for each of those data frames, but I keep throwing errors when I tried methods from similar answers:
The setup is something like this:
a <- list(*a series of data frames that each have a column named DIM*)
dim_loc <- lapply(1:length(a), function(x){paste0("a[[", x, "]]$DIM")}
Eventually, I'll want to write something like results <- lapply(dim_loc, *some function on the DIMs*)
However, when I try get(dim_loc[[1]]), say, I get an error: Error in get(dim_loc[[1]]) : object 'a[[1]]$DIM' not found
But I can return values from function(a[[1]]$DIM) all day long. It's there.
I've tried working around this by using as.name() in the dim_loc assignment, but that doesn't seem to do the trick either.
I'm curious 1. what's up with get(), and 2. if there's a better solution. I'm constraining myself to the apply family of functions because I want to try to get out of the for-loop habit, and this name-as-list method seems to be preferred based on something like R- how to dynamically name data frames?, but I'd be interested in other, more elegant solutions, too.
I'd say that if you want to modify an object in place you are better off using a for loop since lapply would require the <<- assignment symbol (<- doesn't work on lapply`). Like so:
set.seed(1)
aList <- list(cars = mtcars, iris = iris)
for(i in seq_along(aList)){
aList[[i]][["newcol"]] <- runif(nrow(aList[[i]]))
}
As opposed to...
invisible(
lapply(seq_along(aList), function(x){
aList[[x]][["newcol"]] <<- runif(nrow(aList[[x]]))
})
)
You have to use invisible() otherwise lapply would print the output on the console. The <<- assigns the vector runif(...) to the new created column.
If you want to produce another set of data.frames using lapply then you do:
lapply(seq_along(aList), function(x){
aList[[x]][["newcol"]] <- runif(nrow(aList[[x]]))
return(aList[[x]])
})
Also, may I suggest the use of seq_along(list) in lapply and for loops as opposed to 1:length(list) since it avoids unexpected behavior such as:
# no length list
seq_along(list()) # prints integer(0)
1:length(list()) # prints 1 0.
I have 20 unique linear models created from 1 dataset. Each one was created by:
mymodel1 <- lm(y ~ x1 + etc, data=mydata)
Now all I want to do is create a list of the output of a command on all 20 models, e.g. something like:
summary(mymodel[i])$adj
for i=1,2,...,20
It's probably obvious, but I'm not finding anything on this.
Is this the best way to act on 20 variable names that change by a positive integer?
for (i in 1:20) print(somefunction(eval(parse(text=paste0("model", i))))$adj)
This should return a vector of items in your workspace that inherit from class of 'lm":
lm.names <- ls()[ sapply( ls(), function(x) 'lm' %in% class(get(x) ))]
This will return a list of summary items from all of them.
sapply( lm.names, function(x) summary( get(x) )
Notice the use of get (twice). The ls function returns the names of object but neither as the objects themselves nor as true R names, but rather as a character vector. You might want to look carefully at the "Value" section of ?summary.lm, because it's a list and perhaps you only want a few items form that list?
I am working with a list of lm models. Let's create a small example of that:
set.seed(1234)
mydata <- matrix(rnorm(40),ncol=4)
modlist <- list()
for (i in 1:3) {
modlist[[i]] <- lm(mydata[,1] ~ mydata[,i+1])
}
In reality there about 50 models. If you print the modlist object, you'll notice that the call attribute for each model is generic, namely lm(formula = mydata[, 1] ~ mydata[, i + 1]). As later subsets of this list will be needed, I would like to have the convenience to see the name of the dependent variable in each model, assigning that name to the respective call attribute:
modlist[[1]]$call <- "Factor 1"
One can see that the model call has changed to "Factor 1" in the first element of modlist. Let us say I have a vector of names, which I would like to assign:
modnames <- paste0("Factor",1:3)
It would be, of course, possible to assign the respective value of that vector to the respective model in the list, e.g.:
for (i in 1:3) {
modlist[[i]]$call <- modnames[i]
}
Is there a vectorized version of this? I suspect it will be mapply, but I can't figure out how to combine the assignment operator with extracting the respective element of the list, i.e. [[(). More of a purist anti-loop premature optimization exercise, but still :) Thank you!