Problem in the function "niche.overlap" of the Phyloclim R Package - r

I'm trying to use the niche.overlap function by inputting a pno object obtained in the phyloclim package:
library(phyloclim)
x <- pno(path_bioclim = "C:\\Users\\test phyloclim 2\\Nova pasta (3)\\bio2.asc",
path_model = "C:\\Users\\Nova pasta (4)",
subset = NULL , bin_width = 1, bin_number = 100)
niche.overlap(x)
I expect to get a matrix but instead I get got the following error:
Error in niche.overlap(x) : object 'DI' not found

One must only export the object as a .csv file, and then import again as a table. It should work fine.

Related

Error while using tm1r to send dataset. How to debug this issue?

I am currently developing a database integrated forecasting tool for a costumer. I am using mainly R and TM1 Persepctives. To connect R with tm1 I use tm1r. For Data import from tm1 to r it works fine.
However, when I am trying to write back the calculated forecast from R to tm1, I run into problems.
I will give you some reprex data, so you can have a look at the output. If I use "tm2_send_data" instead of "tm1_send_dataset" it works fine too.
The latter function gives me the Error :
"Error in if (is.character(txt) && length(txt) == 1 && nchar(txt, type = "bytes") < :
missing value where TRUE/FALSE needed"
I have no clue, what this is supposed to mean! I tried some formatting of the data types, without any effect.
library(tm1r)
# data
values <- data.frame(fake_values =
c(105,147,159,232,312,337,285,188,257,10,98,27)
)
date_stamps <- c("2021001","2021002","2021003","2021004","2021005","2021006","2021007","2021008","2021009","2021010","2021011","2021012")
rownames(values) = date_stamps
# Send dataset to TM1
con_obj <- tm1_connection("localhost", "8840", "test_admin", "")
tm1_send_dataset(
con_obj,
valueset = values, cube = "pvl_FORECAST_HILFSWÜRFEL",
rowdim = "PVL_test_Zeit", coldim = "pvl_Produkt",
titledim1 = "DATENART", titleel1 = "FC",
titledim2 = "Version", titleel2 = 'Version_Bearbeitung',
titledim3 = "FC-Scheibe", titleel3 = "ML_FC_2021",
titledim4 = "PVL_test_Kunde", titleel4 = "MGR_domestic_D",
titledim5 = "PVL_test_Measure", titleel5 = "Menge_EA"
)

Using an R function to hash values produces a repeating value across rows

I'm using the following query:
let
Source = {1..5},
#"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), {"Numbers"}, null, ExtraValues.Error),
#"Added Custom" = Table.AddColumn(#"Converted to Table", "Letters", each Character.FromNumber([Numbers] + 64)),
#"Run R script" = R.Execute("# 'dataset' holds the input data for this script#(lf)#(lf)library(""digest"")#(lf)#(lf)dataset$SuffixedLetters <- paste(dataset$Letters, ""_suffix"")#(lf)dataset$HashedLetters <- digest(dataset$Letters, ""md5"", serialize = TRUE)#(lf)output<-dataset",[dataset=#"Added Custom"]),
output = #"Run R script"{[Name="output"]}[Value]
in
output
which leads to the resulting table:
And the here is the R script with better formatting:
# 'dataset' holds the input data for this script
library("digest")
dataset$SuffixedLetters <- paste(dataset$Letters, "_suffix")
dataset$HashedLetters <- digest(dataset$Letters, "md5", serialize = TRUE)
output<-dataset
The 'paste' function appears to iterate over rows and resolve on each row with the new input. But the 'digest' function only appears to return the first value in the table across all rows.
I don't know why the behavior of the two functions would seem to operate differently. Can anyone advise how to get the 'HashedLetters' column to resolve using the values from each row instead of just the initial one?
Use:
dataset$HashedLetters <- sapply(dataset$Letters, digest, algo = "md5", serialize = TRUE)
digest works on a whole object at a time, not individual elements of a vector.
vec <- letters[1:3]
digest::digest(vec, algo="md5", serialize=TRUE)
# [1] "38ce1fe9e19a222505e693e8bdd8aeec"
sapply(vec, digest::digest, algo="md5", serialize=TRUE)
# a b c
# "127a2ec00989b9f7faf671ed470be7f8" "ddf100612805359cd81fdc5ce3b9fbba" "6e7a8c1c098e8817e3df3fd1b21149d1"

Error in shapefile$shp : $ operator is invalid for atomic vectors- DSsim package R

I am trying to add a shapefile, when making a region for the DSsim package in R. But I keep getting the error code- Error in shapefile$shp : $ operator is invalid for atomic vectors. Can anyone help me?
The code I have is as follows:
library(DSsim)
file.path("C:/Users/user/Desktop/Lorna/Distance/WITH VARIABLES/C3p", "C3p.shp")
C3P <- shapefiles::read.shp("C3p.shp")
region.label <- make.region(region.name = "C3P", strata.name = 0, units = "km", area = 51.02, shapefile=("C3p.shp"), check.LinkID = TRUE)
The help for make.region says:
shapefile: a shapefile object of the region loaded into R using
‘read.shapefile(shape.name)’ from the shapefiles library.
but you have done:
region.label <- make.region(...., shapefile=("C3p.shp"), ....)
which is the name of the shapefile, in parentheses (these brackets).
Try using read.shapefiles instead of read.shp:
C3P <- shapefiles::read.shapefile(
"C:/Users/user/Desktop/Lorna/Distance/WITH VARIABLES/C3p"
)
Assuming the path to your shapefile is "C:/Users/user/Desktop/Lorna/Distance/WITH VARIABLES/C3p.shp" and similarly for C3p.shx etc.
and then:
region.label <- make.region(
region.name = "C3P",
strata.name = 0,
units = "km",
area = 51.02,
shapefile=C3P, # the object you created two lines previous
check.LinkID = TRUE)

Gini Index in R

I am trying to calculate the Gini index for each row of my database. Each row is a customer and each column is a monthly session. So what i need to do is to add a column with the Gini index by row, for each customer throughout the 12 months.
See example attached
I found some examples online and did this:
Gini_index <- apply(DT_file[,c('sessions_201607_pct','sessions_201608_pct', 'sessions_201609_pct','sessions_201610_pct','sessions_201611_pct','sessions_201612_pct','sessions_201701_pct','sessions_201702_pct','sessions_201703_pct','sessions_201704_pct','sessions_201705_pct','sessions_201706_pct')], 1, gini)
However, I get the following error:
Error in match.fun(FUN) : object 'gini' not found
I have installed both Ineq and Reldist (and libraries) so I don't know why this isn't working.
Try to do this to have your gini's coeff by column :
library(ineq)
coeff= NULL
for (i in colnames(your_data[,-1])){
coeff= c(coeff,round(ineq(your_data[,i],type = 'Gini'),4))
}
data_coeff = data.frame(cbind(coeff,colnames(your_data[,-1])))
colnames(data_coeff) = c("Coeff","Colnames")
If you want it by for each rows try this :
your_new_data = as.data.frame(t(your_data[,-1]), row.names =T)
colnames(your_new_data) = your_data[,1]
ind = NULL
for (i in colnames(your_new_data)){
ind = c(ind,round(ineq(your_new_data[,i],type = 'Gini'),4))
}
data_coeff= data.frame(cbind(ind,colnames(your_new_data)))
colnames(data_coeff) = c("Coeff","customer")
Finaly you add your coeffs at the end of your data_frame with a merge for instance :
your_data_final = merge(your_data,data_coeff, by = "customer" )

how to use fb_insights function to import data from facebook using R

I am trying to use fbRads library to import data from facebook using R.
https://cran.r-project.org/web/packages/fbRads/fbRads.pdf
I have a valid fbaccount object returned by fb_init function and then I am trying to use exactly the same example mentioned here in the docs:
l <- fb_insights(fbacc, date_preset = 'today', level = 'ad')
library(rlist)
list.stack(list.select(l, date_start, date_stop, ad_id, total_actions,
total_unique_actions, total_action_value, impressions, unique_impressions,
social_impressions, unique_social_impressions, clicks, unique_clicks,
social_clicks, unique_social_clicks, spend, frequency, deeplink_clicks,
app_store_clicks, website_clicks, reach, social_reach, ctr, unique_ctr,
cpc, cpm, cpp, cost_per_total_action, cost_per_unique_click,
relevance_score = relevance_score$score))
In variable l, I get:
>print(l)
[[1]]
list()
and then
>list.stack(list.select(l, date_start, date_stop, ad_id, total_actions,
+ total_unique_actions, total_action_value, impression .... [TRUNCATED]
Error in eval(expr, envir, enclos) : object 'date_start' not found
What seems to be not working here? How do I fix this?
I managed to get Fb Ads data using the following command fb_insights(fbacc, level='ad', time_range = '{"since":"2015-08-01","until":"2016-06-31"}'), when the time interval wasn't defined by the time_range I couldn't get the data.

Resources