predict.yai error "no observations"
Closed this issue · 3 comments
candelas762 commented
I am trying to use the funtion "predict.yai" on new data but I get an error saying "Error in newtargets(object, newdata, al$k, al$ann) : no observations". I am not sure what the error means or what is causing the error. I have tried to find examples for predicting on new data but I could not find any.
This is a small reproducible example based on the data MoscowMtJoe:
data("MoscowMtStJoe")
x <- MoscowMtStJoe[, c("EASTING", "NORTHING", "ELEVMEAN",
"SLPMEAN", "ASPMEAN", "INTMEAN", "HTMEAN", "CCMEAN")]
x[, 5] <- (1 - cos((x[, 5] - 30) * pi/180))/2
names(x)[5] = "TrASP"
y <- MoscowMtStJoe[, c(1, 9, 12, 14, 18)]
# Create yai object
mal <- yai(x = x, y = y, method = "mahalanobis")
# Create a new dataset with 30 observations based on original x but adding a bit of variance
nd = x[sample(rownames(x), nrow(x)),]+runif(nrow(x), min = -0.1, max = 0.1)
nd = nd[1:30,]
rownames(nd) = 1:nrow(nd)
# predict
predict.yai(object = mal, newdata = nd)
Deleted user commented
What are the row names on the original data? yaImpute uses row names to
identify observations. So, if the row names of the original training data
are named, 1:n, the first n observations in your new data will be
considered to be identical to the originals and therefore not be "new". If
this is the case, I suggest you change your code so that the row names of
the "new data" are formed some other way, like:
rownames(x)=paste0("new",1:n)
I hope this is the issue because the fix is simple!
Cheers, Nick
…On Mon, Jan 29, 2024 at 6:19 AM candelas762 ***@***.***> wrote:
I am trying to use the funtion "predict.yai" on new data but I get an
error saying "Error in newtargets(object, newdata, al$k, al$ann) : no
observations". I am not sure what the error means or what is causing the
error. I have tried to find examples for predicting on new data but I could
not find any.
This is a small reproducible example based on the data MoscowMtJoe:
data("MoscowMtStJoe")
x <- MoscowMtStJoe[, c("EASTING", "NORTHING", "ELEVMEAN",
"SLPMEAN", "ASPMEAN", "INTMEAN", "HTMEAN", "CCMEAN")]
x[, 5] <- (1 - cos((x[, 5] - 30) * pi/180))/2
names(x)[5] = "TrASP"
y <- MoscowMtStJoe[, c(1, 9, 12, 14, 18)]
# Create yai object
mal <- yai(x = x, y = y, method = "mahalanobis")
# Create a new dataset with 30 observations based on original x but adding a bit of variance
nd = x[sample(rownames(x), nrow(x)),]+runif(nrow(x), min = -0.1, max = 0.1)
nd = nd[1:30,]
rownames(nd) = 1:nrow(nd)
# predict
predict.yai(object = mal, newdata = nd)
—
Reply to this email directly, view it on GitHub
<#1>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABVTTTOU5QKL374BISGHVJTYQ6VW3AVCNFSM6AAAAABCPQW576VHI2DSMVQWIX3LMV43ASLTON2WKOZSGEYDKNRQGY3TQNY>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
--
Nicholas L. Crookston
Forestry Research Consultant
Moscow Idaho USA
jeffreyevans commented
Of course Nick nailed the issue. I change the rowname assignment in your code so that it was not duplicate with the references and it worked.
rownames(nd) = paste0("n",1:nrow(nd))
candelas762 commented
Great! It worked and computed the predictions. Thanks!
However, I have another question. I will post it in a separate issue.