The following is the input I've put in R. I'm just wondering do i need to update R or have i done something wrong?
attach(crimedat)
crimedata <- crimedat[,-2]
lm1 <- lm(Expenditure+YouthUnemploy+MatureUnemploy+Wage~CrimeRate)
vif(lm1)
Error in vif(lm1) : could not find function "vif"
Related
Total R noob here, trying to figure out how to implement mice package to account for NAs in my dataset.
This is my code so far (i left out the unimportant stuff like trimming the data set down to relevant variables, recoding etc.)
install.packages("haven")
install.packages("survey")
library(haven)
library(data.table)
library(survey)
library(car)
dat <- read_dta("ZA5270_v2-0-0.dta")
dat_wght <- svydesign(ids= ~1, data=dat, weights =~wghtpew)
install.packages("mice")
library(mice)
dat_wght[["variables"]]$sex = as.factor(dat_wght[["variables"]]$sex)
dat_imp <- mice(dat_wght[["variables"]], m=5, maxit=10)
The error message I get is:
iter imp variable
1 1 px03Error in `t()`:
! `t.haven_labelled()` not supported.
I already did some research and apparantly it has to do with label values since haven package causes lots of weird problems. I already tried to remove all label values with sapply(dat_wght[["variables"]], haven::zap_labels)but the error still occurs (same when I try it with remove_val_labels()) Does anyone know how to solve this problem?
I'm really grateful for every single piece of advice :) Thanks in advance!
I'm trying to wrap the car::boxCox function into a homemade function so I can mapply it to a list of datasets. I'm using the boxCox function from the car package and not the MASS package because I want to use the family="yjPower". My problem is weird and it's either something fondamental I don't understand or some kind of bug. Here is a reproducible example:
library(car)
le.mod <- function(val.gold,val.bad){
donn <- data.frame(val.gold,val.bad)
res.lm <- lm(val.gold ~ val.bad, data=donn)
bcres <- boxCox(res.lm, family="yjPower", plotit=F)
lambda <- bcres$x[which.max(bcres$y)]
donn$val.bad.t <- donn$val.bad^lambda
res.lm <- lm(val.gold ~ val.bad.t, data=donn)
list(res.lm=res.lm, lambda = lambda)
}
xx <- runif(1000,1,100)
xxt1 <- xx^0.6 + runif(1000,1,10)
yy <- 2*xx + 10 + rnorm(1000,0,2)
le.mod(yy,xxt1)
This gives me the error message:
## Error in is.data.frame(data) : object 'donn' not found
I pin-pointed the problem to the line:
bcres <- boxCox(res.lm, family="yjPower", plotit=F)
boxCox is suppose to be able to take a lm class object, it just doesn't find the associated data that were created 2 lines before.
It works well outside of the function le.mod(). It's probably a problem related to environment management, the boxCox fonction looking for "donn" in the global environment but not finding it and for a reason I ignore not looking for it in the function specific environment.
Anybody have an idea to fix this or explain to me what I don't understand here? I've been turning my head over this problem for days and I can't get it working.
Thanks
I've found the answer (!), however I can't understand the reason of the behaviour so if somebody have an explanation, don't hesitate to post it.
The solution by adding y=TRUE in the second line of the function:
res.lm <- lm(val.gold ~ val.bad, data=donn,y=TRUE)
For some reasons, this allows it to get throught.
I'm running a logit model using the zelig package in R.
m3 <- zelig (newdata$bes.Turnout ~ civicduty + newdata$bes.CivicDuty2+newdata$bes.Age, model="logit", data=newdata)
newdata$bes.Turnout is binary, the rest are numerical.
I try to set the values of x.
mcos<- median(newdata$bes.CivicDuty2)
civicmean<-mean(civicduty)
mage<-mean(newdata$bes.Age)
test<-setx(m3, civicduty=civicmean, newdata$bes.CivicDuty2=mcos, newdata$bes.Age=mage)
I get the following error message:
Error: unexpected '=' in "test<-setx(m3, civicduty=civicmean, newdata$bes.CivicDuty2="
I have tried the same code for the survival data in Zelig, and here the setx worked.
Any ideas what I am doing wrong and how to fix it?
That call to setx doesn't make sense to me. My best guess is that you want something like
test<-setx(m3,
civicduty = mean(newdata$civicduty),
bes.CivicDuty2 = mean(newdata$bes.CivicDuty2,
bes.Age = mean(newdata$bes.Age))
I'm building a segmented regression model using R's Segmented package.
I was able to create the model but have trouble using the predict.segmented function. It always throws an error saying "subscript out of bounds"
This is the exact error message:
Error in newdata[[nameZ[i]]] : subscript out of bounds
Traceback just gives this:
1: predict.segmented(seg_model, xtest)
I created a simple case that gives the same error:
require(segmented)
x = c(1:90, 991:1000)
y = c((x[1:10]/2), (x[11:100]*2))
lm_model = lm(y~x)
seg_model = segmented(lm_model, seg.Z=~x, psi=list(x=NA),
control=seg.control(display=FALSE, K=1, random=TRUE))
xtest = c(1:1000)
predict.segmented(seg_model, xtest)
I am starting to think this could be a bug. I'm new to R and not sure how to debug this either. Any help is appreciated!
You are using predict.segemented incorrectly. Like nearly all the predict() functions, your newdata parameter should be a data.frame, not a vector. Also, it needs to have names that match the variables used in your regression. Try
predict.segmented(seg_model, data.frame(x=xtest))
instead. When using a function for the first time, be sure the read the help page (?predict.segmented) to know what the function expects for each of the parameters.
I tried to write the formula to get the slope using R and Tableau integration.
My formula within the calculated field shows to be a valid one. However, when I try to plot the same, I get an error. The formula i am using is as follows:
SCRIPT_REAL("mydata <- data.frame(cbind(yy = .arg1, xx = .arg2)); fit <- lm(yy ~ xx,new data=mydata); fit$coeff[[2]]",(avg([Revenue Growth])),(avg([WTI]) ))
The error i receive is :
Any help with the same would be appreciated.
Thanks.
Try this:
SCRIPT_REAL(
'mydata<- data.frame(yy=.arg1, xx=.arg2);
fit <- lm(yy~xx,mydata)$coefficients[2]',
AVG([Revenue Growth],
AVG([WTI])
)