Trying to do a reset test in Rstudio - r

I'm trying to do a reset test in Rstudio. Im new to this.
This is my code:
model1 <- lm(inntekt ~ arbeidstid + kvinne + alder + leder + utd_hoy, data = d)
summary(model1)
install.packages("lmtest")
library("lmtest")
resettest(lm, power = 2:3, type = "regressor", data = d)
When i am trying to run the test i get this:
resettest(lm, power = 2:3, type = "regressor", data = d)
Error: object of type 'closure' is not subsettable
Can somebody pleas help me? I am happy for all answers :)

May be because you should write name of lm object, not just lm :
resettest(model1, #replace lm by model1
power = 2:3, type = "regressor", data = d)

Related

How to implement your own nonlinear function in nlmer in R?

I am trying to implement a new nonlinear function to use in nlmer function in lme4 package. But I'm not sure what the problem is. This is the first time I'm trying to use nlmer but I'm following all the instructions I've found on the internet. The first error is about my dataframe.
data <- read.csv(paste("C:/Users/oguz/Desktop/Runs4SiteModels/db/", "DB4NLSiteModel", Periods[i],".txt", sep=""), sep = "", header = TRUE)
psa_rock <- data$PSAr
nparams <- c("c")
nonl_fn <- deriv(~ log(( psa_rock + c)/c),
namevec = c("c"),
function.arg=c("c", psa_rock))
fm <- nlmer(log(data$PSAm) ~ nonl_fn(c, psa_rock) ~ 1 + data$M1 + data$M3 + data$M85 + data$Nflag + data$Rflag + data$FDepth +
data$Dist1 + data$Dist3 + data$VN + (exp(-1*exp(2*log(data$Vs)- 11)) * log((data$PSAr + c) / c) ) +
(1|data$EQID) + (1|data$STID), data=data, start=c(c=0.1))
When I run this code, I'm getting the following error:
Error in model.frame.default(data = data, drop.unused.levels = TRUE, formula = log(data$PSAm) ~ :
invalid type (list) for variable 'data'
which I wasn't getting it while using lmer function (of course without the nonlinear function). That's why I'm thinking my problem is not about my dataframe.
Other issue that I couldn't stop thinking about, the part in the fixed-effects:
(exp(-1*exp(2*log(data$Vs)- 11)) * log((data$PSAr + c) / c) )
as you can see my nonlinear function also takes a part in my fixed-effects formula and I'm not quite sure how to implement that. I hope my way is correct but because of my first problem, I couldn't find an opportunity to test that.

Ldahist(0 function Object not found

I am learning LDA using Wine data.
The dependent variable is called "Type" with 13 predictors.
wine.lda<-lda(Type ~., data = wine)
wine.lda$svd
wine.lda.values <- predict(wine.lda)
ldahist(data = wine.lda.values$x[,1], g=Type)
######OUTPUTS#########
> wine.lda<-lda(Type ~., data = wine)
> wine.lda$svd
[1] 28.18958 19.00634
> wine.lda.values <- predict(wine.lda)
> ldahist(data = wine.lda.values$x[,1], g=Type)
Error in ldahist(data = wine.lda.values$x[, 1], g = Type) :
object 'Type' not found
Everything was just fine. But when I tried to plot histogram. It gave me this error "object not found". I checked my code several times, but i couldnt find the reason.
Any help would be appreciated. Thanks!

for loop in ctree [R]

I want to run a decision tree for each variable in my dataframe, so I'm using this:
results_cont = list()
for (i in 2:(ncol(DATA)-1)) {
current_var = colnames(DATA[i])
current_result = ctree(TARGET ~ current_var, DATA, control = ctrl)
results_cont[[i]] = current_result
}
Where DATA is a dataframe where the first column is the ID and the last column (TARGET) is my binary Target.
I keep getting this error:
Error in trafo(data = data, numeric_trafo = numeric_trafo, factor_trafo = factor_trafo, :
data class “character” is not supported
But I don't have any character in mi dataframe.
Is there anything wrong with my loop or something else ?
Thank you guys.
Since you do not provide data, I have not tested this, but I believe your problem is the line
current_result = ctree(TARGET ~ current_var, DATA, control = ctrl)
This is not working because current_var is just a character string. You need to build the formula as a string and then convert it to a formula - like this:
current_var = colnames(DATA[i])
FORM = as.formula(paste("TARGET ~ ", current_var))
current_result = ctree(FORM, DATA, control = ctrl)

R Error: "object of type 'closure' is not subsettable"

I am receiving the following message when I run my code. I am attempting to perform a mediation analysis.
"Error in new$meddepression : object of type 'closure' is not subsettable".
I am very new to programming and to R. Any help or advice would be greatly appreciated! Below is my code.
doEffectDecomp = function(d)
{
d$actlim2Temp=d$actlim2
mdepression = glm(depression ~ actlim2Temp, family=binomial(), data=d)
d1 = d2 = d
d1$meddepression = d1$actlim2
d2$meddepression = !d2$actlim2
newd = rbind(d1,d2)
newd$actlim2Temp = newd$actlim2
w = predict(mdepression, newdata = newd, type='response')
direct = ifelse(newd$depression, w, 1-w)
newd$actlim2Temp <- new$meddepression
w = predict(mdepression, newdata = newd, type='response')
indirect = ifelse(newd$depression, w, 1-w)
newd$Wdepression = indirect/direct
newd$W = newd$Wdepression
cox=coxph(Surv(days_in_study, censor) ~ meddepression + actlim2, weight=W,
data=newd)
TE=exp(sum(coef(cox)[c('actlim2', 'meddepression')]))
DE=exp(unname(coef(cox)['actlim2']))
IE=exp(sum(coef(cox)[c('meddepression')]))
PM = log(IE)/log(TE)
return(c(exp(coef(cox)), TE=TE, DE=DE, IE=IE, PM=PM))
}
doEffectDecomp(d)
Thanks in advance for your help,
Simran
There was a typo in the code, I was missing a "d". new$meddepression should have read newd$meddepression.
Thank you very much for all your input, it is greatly appreciated.

Trying to create an AICc table but receiving aictab error message

I'm trying to create an AICc table for a number of multinomial logistic regression models. However, when I try to run the line:
aictab(cand.set = Cand.mod, modnames = Modnames)
I get the error message:
Error in print(aictab(cand.set = Cand.models, modnames = Modnames)) :
could not find function "aictab"
The script I am using is:
install.packages("AICcmodavg")
dd$reaction2 <- relevel(dd$reaction, ref="nr")
Cand.mod<-list()
Cand.mod[[1]]<-multinom(reaction2~ camera + light, data = dd)
Cand.mod[[2]]<-multinom(reaction2~ camera + gender, data = dd)
Modnames <-c("fm1", "fm2", "fm3")
aictab(cand.set = Cand.mod, modnames = Modnames)
evidence(aictab(cand.set = Cand.mod, modnames = Modnames))
confset(cand.set = Cand.mod, modnames = Modnames, method= "raw")
I'm just wondering if anyone has any ideas where I am going wrong?
Thanks so much!!

Resources