Check for Null Values in R tis - r

I rarely use R, but need for a helper function that somebody has given me. I have the following line:
league_model_19 <- gam(SHOT_MADE_FLAG ~ ti(LOC_X) + ti(LOC_Y) + ti(LOC_X, LOC_Y), data = shots)
But, when I run it, the following error message is displayed:
Error in ti.default(LOC_X) :
'tif' and 'freq' cannot both be NULL if 'x' is not a ti
I saw online somebody write code along the lines of:
if(is.null(tif)){
if(is.null(freq))
stop("'tif' and 'freq' cannot both be NULL if 'x' is not a ti")
else tif <- freq2tif(freq)
}
But I don't know how to integrate this into my current line, or if there is a more efficient way to do this. Does anybody know how to get rid of this error message so I can proceed with my code? Let me know if more details are needed.
Edit: Added minimal dataframe to reproduce error (player_id and player_name aren't used here, just left in there). This is the data that is in 'shots' as called in the line of code I shared:
PLAYER_ID,PLAYER_NAME,LOC_X,LOC_Y,SHOT_MADE_FLAG
1627759,Jaylen Brown,-20,19,0
1627759,Jaylen Brown,-29,168,1
1627759,Jaylen Brown,7,30,1
1627759,Jaylen Brown,9,6,0
1627759,Jaylen Brown,-176,186,0

Sounds like you either don't have the mgcv package loaded, or you have the tis package loaded after mgcv and the former is masking the latter.
If you didn't load mgcv, add library('mgcv') to your script before fitting the gam().
If you have mgcv loaded but also loaded the tis package, both packages provide a ti() function and it seems the tis one is being found first on the search path. Your best option is to use the fully qualified name for the ti() so that you inform R as to which ti() you want to use.
Try:
league_model_19 <- gam(SHOT_MADE_FLAG ~ mgcv::ti(LOC_X) +
mgcv::ti(LOC_Y) +
mgcv::ti(LOC_X, LOC_Y),
data = shots)
You could also change the script to load mgcv after tis but this might cause later problems when you use the ti() from the tis package; in which case you could use tis:ti() in those places.

Related

How to fix lmer error: "Error in as(value, fieldClass, strict=FALSE) :"?

I'm getting a strange error when I run an lmer function in r.
I've tried changing the variable types (all of them are numeric or factor) and removing the NA before analysis, but nothing seems to work.
model_1 <- lmer(Q14 ~ gender * time + (1|OMID), data=data)
summary(model_1)
Specifically, my error message reads:
Error in as(value, fieldClass, strict = FALSE) :
internal problem in as(): “labelled” is(object, "numeric") is TRUE, but the metadata asserts that the 'is' relation is FALSE
Not sure why this is happening, but I can't seem to find any answers for it. Any help would be appreciated.
Thanks!
I think lmer has a problem with 'labelled' data. If you un-label the predictors it should work fine.
I had the same error: the code for the lme-formula worked perfectly fine and one day I encountered that error. The solution in my case was simply to restart the R session, reload the data - in my case from SPSS via library("haven")::read.sps and after that load library("lme4") and execute the lme-formula.
So, if the formula worked before without any error, maybe just clean the project environment and re-run the most crucial code without any additional packages loaded. Maybe it's just some "cross-contamination" between packages or an unwanted effect of any package on the dataframe.

Functions can't be found, but the packages are certainly installed and loaded

I'm trying to predict values with a linear mixed model and new data, but I keep getting errors that both functions I'm trying (predict.lme from nlme, and predict.merMod from lme4) do not exist. The packages are installed and loaded.
I first tried using predict.lme (nlme). I note that I've installed and loaded the package, since obviously this is a reason the function may not be found.
But I get this error:
Error in predict.lme(object = PlotModel.best, newdata = PlotInvData_predict,:
could not find function "predict.lme"
I've had random problems with nlme before, so I decide to try predict.merMod (lme4), but I get the same error that that function can't be found. I thought maybe those functions can't handle the interval argument that the regular predict function can. I get rid of that, and it still doesn't work. I tested other functions in those packages, and they work just fine. So something is wrong with my workflow, but I can't figure out what. It's the same workflow as with the regular predict function, which I've used just fine.
What am I doing wrong?
Here's the new data:
PlotInvData_predict <- read.csv(file="D:/ThesisPart2/Data/PlotInvData_predict.csv", header=TRUE, sep=",")
Heres the model:
PlotModel.best <- lmer(d_InvCov ~ TimeSinceDist + UnitArea_ha + (1|MgmtSame)+ (1|LandMgmt.1), PlotInvData)
Then I first tried using predict.lme (nlme). I note that I've installed and loaded the package, since obviously this is a reason the function may not be found.
install.packages("nlme")
library(nlme)
p_bd <- predict.lme(object=PlotModel.best, newdata=PlotInvData_predict, interval="confidence")
Here's the error, again:
Error in predict.lme(object = PlotModel.best, newdata = PlotInvData_predict, :
could not find function "predict.lme"
When using the regular predict function with a different model (linear model), I would get something like this as a result:
fit lwr upr
1 1.098959 0.5803632 1.617556
2 1.156005 0.6627035 1.649306
3 1.213050 0.7408797 1.685220
4 1.270095 0.8143122 1.725879
5 1.327141 0.8824762 1.771805
6 1.384186 0.9449715 1.823401
7 1.441231 1.0015871 1.880875

Where did the forecast.Holtwinters go in R 3.4.3?

I'm using R Studio based on R 3.4.3. However, when I tried to call the forecast.HoltWinters function, R told me that "could not find function "forecast.HoltWinters"". Inspect the installed package (v8.2) told me that it's true, there is no forecast.HoltWinters. But the manual in https://cran.r-project.org/web/packages/forecast/ clearly stated that forecast.HoltWinters is still available.
I have also tried stats::HoldWinters, but it's working wrong. The code run fine on another computer, but it couldn't run at all on mine. Is there any solution?
Here is the code. Book2.csv has enough data to last more than 3 periods.
dltt <- read.csv("book2.csv", header = TRUE)
dltt.ts <- ts(dltt$Total, frequency=12, start=c(2014,4))
dltt.ts.hw <- HoltWinters(dltt.ts)
library(forecast)
dltt.ts.hw.fc <- forecast.HoltWinters(dltt.ts.hw) //Error as soon as I run this line
Fit a HoltWinters model using the HoltWinters function and then use forecast. Its all in the help for HoltWinters and forecast, namely "The function invokes particular _methods_ which depend on the class of the first argument". I'll copy the guts of it here:
m <- HoltWinters(co2)
forecast(m)
Note this will call the non-exported forecast.HoltWinters function, which you should never call directly using triple-colon notation as some may suggest.

Getting an error could not find function in a for loop

I'm running the following code:
dat1 <- returns
for (j in 1:12) set(dat1, j = j, value = wind(dat1[[j]]))
And getting the following error message:
Error in wind(dat1[[j]]) : could not find function "wind"
My search for a solution mainly involves packages that aren't properly installed. I'm not 100% sure but I think it isn't related to that.
Best
To check if the function is really loaded into the namespace you have to try to
print the function:
print(wind)
Error in print(wind) : object 'wind' not found
You have to look if the package is correctly loaded
library("foo")
Check package dependency in case of error.
I use the findFn-function in sos-package to search for function names. Unfortunately dataset names also appear in the same column:
install.packages("sos")
library(sos)
findFn("wind")
There is a wind function (or dataset name) in packages: gstat, ismev, NPCirc, BAMBI, ggmap, gcookbook, CircOutlier, plotly, and circular.

Error message in lme4::glmer: " 'what' must be a character string or a function"

I am running a multi-level model. I use the following commands with validatedRS6 as the outcome, random as the predictor and clustno as the random effects variable.
new<-as.data.frame(read.delim("BABEX.dat", header=TRUE))
install.packages("lme4")
library(lme4)
model1<- glmer(validatedRS6 ~ random + (1|clustno), data=new, family=binomial("logit"), nAGQ = 1L)
However, I get the following error
Error in do.call(new, c(list(Class = "glmResp", family = family), ll[setdiff(names(ll), :
'what' must be a character string or a function
I have absolutely no idea what has gone wrong and have searched the internet. I am sorry but I cannot provide the data as it is from an intervention which has yet to be published.
(expanded from comment).
Congratulations, you found a bug in lme4! This is fixed now:
https://github.com/lme4/lme4/commit/9c12f002821f9567d5454e2ce3b78076dabffb54
It is caused by having a variable called new in the global environment (deep in the guts of the code, lme4 uses do.call(new,...) and finds your variable new rather than the built-in function new).
You can install a patched version from Github using devtools::install_github() (but you'll need compilation tools etc.). Alternately, there is a very simple workaround -- just call your variable anything other than new (you can't just copy it, i.e. new2 <- new -- you also have to make sure the old version is removed (rm("new"))).

Resources