Why is set.seed not working for mcp R package? - r

I am doing a simple change point analysis using the mcp R package, but my results still vary each time I rerun my code even after including set.seed. Appreciate any help on this! (Below is my code, thank you!)
library(rjags)
library(mcp)
model = list(y~1+x,~0+x,~0+x,~0+x,~0+x,~0+x)
set.seed(42)
fit_mcp = mcp(model, data=hosp_df)
summary(fit_mcp)
plot(fit_mcp)
newdata = data.frame(x = c(2023:2040))
prediction <- fitted(fit_mcp, newdata = newdata)

Related

Cross Validation without caret in R

Since the model (package fastNaiveBayes) that I am using is not in the built-in library of the caret package, I am trying to make a k-fold cross validation in R without using the caret package. Does anyone have a solution to this?
Edit:
Here is my code so far from what I learned on how to do cv without caret. I am very certain something is wrong here.
library(fastNaiveBayes)
k<- 10
outs <- NULL
proportion <- 0.8
for (i in 1:10)
{
split <- sample(1:nrow(data), round(proportion*nrow(data)))
traindata <- data[split,]
testdata <- data[-split,]
y <- traindata$Label
x <- traindata[,0 - 15:ncol(traindata)]
model <- fnb.train(x, y=y, priors = NULL, laplace=0,
distribution = fnb.detect_distribution(x, nrows = nrow(x)))
model
test1 <- testdata[,0 - 15:ncol(testdata)]
pred <- predict(model, newdata = test1)
cm<- table(testdata$Label, pred)
print(confusionMatrix(cm))
}
It gave me 10 different results and I think that's not how it cross validation is supposed to work. I'm an entry-level R learner and I appreciate so much to receive enlightenment from this

R difference between class and DMwR package knn functions?

So I was working on a project in R and I ran into a issue with fitting a KNN model to some data. I was getting different results when I ran the knn from class and kNN from DMwR libraries. I tied using the Weekly data from the psych package but I got similar results. Confusion matrices for the fits give significantly different results as does the strait up comparison between between the predictions.
I am not sure why these two functions are returning different results. Maybe someone can review my sample code and let me know what is going on.
library(ISLR)
WTrain <- subset(Weekly, Year <= 2008)
WTest <- subset(Weekly, Year >= 2009)
library(caret)
library(class)
fitClass <- knn(train = data.matrix(WTrain$Lag2), test = data.matrix(WTest$Lag2), cl=WTrain$Direction, k=5)
confusionMatrix(data = fitClass, reference = WTest$Direction)
library(DMwR)
fitDMwR <- kNN(Direction~Lag2,train = WTrain, test = WTest, norm=FALSE, k=5)
confusionMatrix(table(fitDMwR == 'Down', WTest$Direction =='Down'))
results <- cbind(fitClass,fitDMwR)
head(results)

R predict() returns only fitted values for nls() model

First of all, I would like to mention I am just a beginner in R.
I have encountered a problem when trying to predict data from a model generated by nls(). I fitted the exponential decay function into my data and everything seems to be fine, e.g. I got a decent regression line. However, when I use predict() on a new data set, it returns only fitted values.
My code is:
df = data.frame(Time = c(0,5,15,30), Value = c(1, 0.38484677,0.18679383, 0.06732328))
model <- nls(Value~a*exp(-b*Time), start=list(a=1, b=0.15), data = df)
plot(Value~Time, data = df)
lines(df$Time, predict(model))
newtime <- data.frame(Time = seq(1,20, by = 1))
pr = predict(model, newdata = newtime$Time)
pr
[1] 0.979457389 0.450112312 0.095058637 0.009225664
Could someone explain me please, what I am doing wrong? I know there are here some answers to that problem, but none helped me.
Thank you in advance for your help!
The newdata parameter should be a data.frame with the same names as your input data. When you use newdata = newtime$Time you are actually passing in newtime$Time which is not a data.frame anymore since it 'dropped' down to a vector. You can just pass in newtime like so
pr = predict(model, newdata = newtime)

Understanding how to use nnet in R

This is my first attempt using a machine learning paradigm in R. I'm using a planet data set (url: https://www.kaggle.com/mrisdal/open-exoplanet-catalogue) and I simply want to predict a planet's size based on the size of its Sun. This is the code I currently have, using nnet():
library(nnet)
#Organize data:
cols_to_keep = c(1,4,21)
full_data <- na.omit(read.csv('Planet_Data.csv')[, cols_to_keep])
#Split data:
train_data <- full_data[sample(nrow(full_data), round(nrow(full_data)/2)),]
rownames(train_data) <- 1:nrow(train_data)
test_data <- full_data[!rownames(full_data) %in% rownames(data1),]
rownames(test_data) <- 1:nrow(test_data)
#nnet
nnet_attempt <- nnet(RadiusJpt~HostStarRadiusSlrRad, data=train_data, size=0, linout=TRUE, skip=TRUE, maxNWts=10000, trace=FALSE, maxit=1000, decay=.001)
nnet_newdata <- predict(nnet_attempt, newdata=test_data)
nnet_newdata
When I print nnet_newdata I get a value for each row in my data, but I don't really understand what these values mean. Is this a proper way to use the nnet() package to predict a simple regression?
Thanks
When predict is called for an object with class nnet you will get, by default, the raw output from the nnet model applied to your new dataset. If, instead, yours is a classification problem, you can use type = "class".
See here.

Support vector machine plotting

I was trying to plot a SVM classification. However I encountered a problem that I have no idea how to fix. I looked at documentation and some videos but still stuck. Here is my code
library(ISLR)
svm.oj2 <- svm(Purchase~.,data=OJ,kernel='linear',cost=1,scale = F)
plot(svm.oj2,data=OJ)
Here is the error:
Error in plot.svm(svm.oj2, data = OJ) : missing formula.
Really appreciate any help
I think this is what you are trying to do:
library(ISLR)
library(e1071)
svm.oj2 <- svm(Purchase~.,data=OJ,kernel='linear',cost=1,scale = F)
plot(OJ, col = 1:1000 %in% svm.oj2$index + 1)

Resources