How to calling file R in GAMA

65 views
Skip to first unread message

ttrung...@gmail.com

unread,
Mar 29, 2023, 10:07:11 PM3/29/23
to GAMA
Hi there, I use GAMA 1.8.2 and in my code have a calling R command. But it shows an error.

My code R is the following:
----------------------------------------------------------------------------------
library(sp)
library(maptools)
library(lattice)

# PARAMETERS
n<-length(vectorParam)/3
lighttrap <- data.frame(x=c(1:n), y=c(1:n), density=c(1:n))

lighttrap[,3]<-vectorParam[1:n]
lighttrap[,1]<-vectorParam[(n+1):(2*n)]
lighttrap[,2]<-vectorParam[(2*n+1):(3*n)]

# PROCESSING
coordinates(lighttrap)=~x+y
v_uk = gstat::variogram(density~x+y, lighttrap)
uk_model = gstat::fit.variogram(v_uk, gstat::vgm(1, "Exp", 40000, 0))

##### GET MODEL PARAMETERS#######
model_nugget <- data.frame(uk_model)[1,2]
model_sil <- data.frame(uk_model)[2,2]
model_range <- data.frame(uk_model)[2,3]
model_kappa <- data.frame(uk_model)[2,4]
estimated_vectors <- NULL
sampled_vectors <- NULL
mse <- 0
for(cnt in 1:length(v_uk[,1]))
{
distance <- v_uk[cnt, 2]
estimated_value <- model_sil * (1 - exp(-(distance)/(model_range))) + model_nugget
gamma_value <- v_uk[cnt, 3]
estimated_vectors <- c(estimated_vectors, estimated_value)
sampled_vectors <- c(sampled_vectors, gamma_value)
mse <- mse + (estimated_value - gamma_value) ^ 2
}
result<-mse
----------------------------------------------------------------------------------
My GAMA script is very long. But I take some lines related to calling R. The input of RCode is densityList, this variable is take some step to processing before bring it to R Code:
----------------------------------------------------------------------------------
model MainModel
global skills: [RSkill] {
// Initialzied
init {
.......................
do estimateR;
.......................

}

action estimateR{
...........
list MSEVariogramList;
list densityList;
...........
do startR();
file Rcode <- text_file("../includes/RCode/estimateMSE.R");
unknown resultAfterExcuted;
resultAfterExcuted <- R_eval("vectorParam = " + to_R_data(densityList));

loop command over: Rcode.contents {
resultAfterExcuted <- R_eval(command);
write "Command: " + command;
write "resultAfterExcuted: " + resultAfterExcuted;
}

rs <- resultAfterExcuted;
//
MSEVariogramList[setAt] <- float(rs[0]);



}
}



---------------------------------------------------------------------------------
I have tested with GAMA 1.6 and it works but GAMA 1.8.2, shows an Errors in console:
-------------------------------------------------------------------------------
R>Error: object 'distance' not found

R>In addition:
R>Warning message:

R>In gstat::fit.variogram(v_uk, gstat::vgm(1, "Exp", 40000, 0)) :
R>
 
R> No convergence after 200 iterations: try different initial values?

R>Error in `[.data.frame`(v_uk, cnt, 3) : object 'cnt' not found

R>Error: object 'estimated_value' not found

R>Error: object 'gamma_value' not found

R>Error: object 'estimated_value' not found
-------------------------------------------------------------------------------
And the result always return [0,0,0] value
--------------------------------------------------------------------------------
Please help me
Hope you clear with my question
Thank you very much

Benoit Gaudou

unread,
Mar 30, 2023, 4:06:50 AM3/30/23
to gama-p...@googlegroups.com
Dear,

First of all, I simply tried your script and it does not work.... 

> n<-length(vectorParam)/3
Error: object 'vectorParam' not found

Benoit

--
You received this message because you are subscribed to the Google Groups "GAMA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gama-platfor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gama-platform/151cfdad-5955-468e-86fa-df8b3ab0321dn%40googlegroups.com.
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted

Benoit Gaudou

unread,
Mar 30, 2023, 1:17:21 PM3/30/23
to gama-p...@googlegroups.com
Dear,

Just to let you know, running your model I get :
that is only the beginning of the execution and there are errors that do not seem to be related to GAMA ....
Cheers

Benoit


Command: library(sp)

resultAfterExcuted: ['sp','stats','graphics','grDevices','utils','datasets','methods','base']

R>Checking rgeos availability: FALSE

Please note that 'maptools' will be retired during 2023,

plan transition at your earliest convenience;

some functionality will be moved to 'sp'.

Note: when rgeos is not available, polygon geometry computations in maptools depend on gpclib,

which has a restricted licence. It is disabled by default;

to enable gpclib, type gpclibPermit()

Command: library(maptools)

   resultAfterExcuted: ['maptools','sp','stats','graphics','grDevices','utils','datasets','methods','base']

Command: library(lattice)

   resultAfterExcuted: ['lattice','maptools','sp','stats','graphics','grDevices','utils','datasets','methods','base']

Command: #library(gstat)

   resultAfterExcuted: [NULL ]

Command:

   resultAfterExcuted: null

Command: # PARAMETERS

   resultAfterExcuted: [NULL ]

Command: n<-length(vectorParam)/3

   resultAfterExcuted: [0.3333333333333333]

Command: lighttrap <- data.frame(x=c(1:n), y=c(1:n), density=c(1:n))

   resultAfterExcuted: [[1],[1],[1]]

Command:

   resultAfterExcuted: null

Command: lighttrap[,3]<-vectorParam[1:n]

   resultAfterExcuted:

Command: lighttrap[,1]<-vectorParam[(n+1):(2*n)]

   resultAfterExcuted:

Command: lighttrap[,2]<-vectorParam[(2*n+1):(3*n)]

   resultAfterExcuted:

Command:

   resultAfterExcuted: null

Command: lighttrap

   resultAfterExcuted: ['','','']

Command:

   resultAfterExcuted: null

Command: # PROCESSING

   resultAfterExcuted: [NULL ]

   R>Erreur dans .local(obj, ...) : cannot derive coordinates from non-numeric matrix


Le jeu. 30 mars 2023 à 10:22, ttrung...@gmail.com <ttrung...@gmail.com> a écrit :
Dear Benoit
Yes, I see. If you run my R Code standalone you will get that error cause "vectorParam" is not declared before. So before I read the R script by GAMA, I have assigned "vectorParam" to "densityList" by command "resultAfterExcuted <- R_eval("vectorParam = " + to_R_data(densityList));". And after this step, I can run each line to the for-loop, GAMA show an error like I send you before
Thanks

Vào lúc 15:06:50 UTC+7 ngày Thứ Năm, 30 tháng 3, 2023, Benoit Gaudou đã viết:
Reply all
Reply to author
Forward
Message has been deleted
0 new messages