Add formula into function - r

I have this example data
install.packages('neuralnet')
library(neuralnet)
DV<-runif(20,min=-3,max=3)
RV_1<-runif(20,min=-3,max=3)
RV_2<-runif(20,min=-3,max=3)
formula<-'RV_1+RV_2'
df<-data.frame(DV=DV,RV_1=DV_1,RV2=RV_2)
and I learn the neural network this way
neuralnet(DV~RV_1+RV_2,data=df,hidden=5)
and everything works well.
But if I need to use it in function for more combinations I need to use it like
testfun<-function(x,y){
nnet<<-neuralnet(x~y,data=df,hidden=5)
}
testfun(DV,formula)
Which doesn't work
I've tried these approaches
testfun<-function(x,y){
nnet<<-neuralnet(print(x,quote=FALSE)~print(y,quote=FALSE),data=df,hidden=5)
}
or
testfun<-function(x,y){
nnet<<-neuralnet(as.symbol(x)~as.symbol(y),data=df,hidden=5)
}
or
testfun<-function(x,y){
nnet<<-neuralnet(get(x)~get(y),data=df,hidden=5)
}
But nothing works. The problem is that I cannot change the formula object and I still cannot go trough.
Any advices how to solve this problem?

Try this?
testfun<-function(x,y) {
neuralnet(as.formula(paste(x, "~", y, sep ="")), data=df, hidden=5)
}
nnet <- testfun("var1", "var2")

Related

ifc.js hiding example: hover highlights elements in non-displayed subsets

The issue is with the Hiding example (subset example with checkboxes):
helloworld/main/examples/web-ifc-viewer/hiding/bundle.js
When a subset is not-displayed, hover still highlights the element in the non-displayed subset. The solution as proposed on Discord was to remove the subset model from the array: ifcviewer.context.items.pickableIfcModels,
but it's not clear to me how or where to do this. Thanks for any help.
most probably because " ifcviewer.context.items.pickableIfcModels" is still thinks that you're dealing with the model as a whole not with the subsets,
you need to add this function:
function togglePickable(mesh, isPickable)
pickable = viewer.context.items.pickableIfcModels
if(isPickable) {
pickable.push(mesh)
} else {
const index = pickable.indexof(mesh)
pickable.splice(index, 1)
}
and on setupCheckbox function add this line under
if(checkbox.checked){
..
togglePickable(subset, true)
}else{
...
togglePickable(subset, false)
}
and on setupCategory function add the following:
togglePickable(subset[category], true)
and on your load model function you also need to add:
togglePickable("your model", true)
and it shall work ^^

loop problems make frequency table in R

library(MASS)
with (survey,
for (variable in names(Filter(is.factor, survey))) {
print(table(variable))
}
)
I'd like to make frequency tables for all factor variables.
But, it doesn't work.
need some help.
Here is another solution:
lapply(Filter(is.factor, survey), table)
Here is a slight modification that works.
for (variable in names(Filter(is.factor, survey))) {
print(table(survey[[variable]]))
}
To use with() with a character as you have tried you could do (not necessarily recommended):
with(survey,
for (variable in names(Filter(is.factor, survey))) {
print(table(eval(as.symbol(variable))))
}
)

how to make a loop function with "adonis"?

data(dune)
data(dune.env)
results<-list()
for (i in colnames(dune.env)){
results[[i]]<- adonis(dune ~ i, data=dune.env, permutations=99)
}
When I test each name in colnames(dune.env), it can work.
But it can not work in the loop function above. I think it is due to the i in the loop fuction has " ". How to fix it? Thanks.
I know nothing about adonis, but I do know that formulas are language objects which do not take nicely to being treated as though they were ordinary character objects.
for (i in colnames(dune.env)){
form <- as.formula(paste("dune", i, sep="~"))
results[[i]]<- adonis(form, data=dune.env, permutations=99)
}

Deduplication in R Studio

this is my first R Code, and it is a very simple deduplication, but it is working so slowly I can't believe it! My question is: Is it normal that it is working so slowly or is my code just bad?
Here it is:
file1=c(read.delim("file.txt", header=TRUE))
dedupes<-0
i<-1
n<-1
while (i<=100) {
while (n<=100) {
if (file1$email[i]==file1$email[n] && i!=n) {
#Remember amount of deduces
dedupes=dedupes+1
#Show dedupes
print(file1$email[i]) }
n<-n+1
}
n<-1
i<-i+1
}
#Show amount of dedupes
cat("There are ", dedupes/2, " deduces")
Many thanks in advance,
Saitam
Imbricated loops are well known to be slow in R. You need to vectorize your calculus or use existing optimized functions such as in the suggestion of BondedDust

removing elements of the environment using a loop

I have 16 elements in the environment called Factor1 to Factor16. I would like to remove them automatically. I wrote that and I cannot understand why that's not working...
for(i in 1:16) {
rm(paste0('Factor',i))
}
sorry for this basic question, I am a beginner!
for(i in 1:16) {
rm(list=paste0('Factor',i))
}
although rm(list=paste0('Factor',1:16)) or rm(list=ls(pattern="Factor"))would be more appropriate...

Resources