scholar icon indicating copy to clipboard operation
scholar copied to clipboard

Error in predict_h_index()

Open mgclaros opened this issue 2 years ago • 3 comments

I have updated my R 4.2.2 in my aarch64-apple-darwin20 and I always obtain the same error: predict_h_index("vamErfkAAAAJ") Error in if (is.na(h)) return(NA) : the condition has length > 1

I located the problem in the function when

h <- get_profile(id) is.na(h) id name affiliation total_cites h_index i10_index fields FALSE FALSE FALSE FALSE FALSE FALSE FALSE homepage coauthors available not_available FALSE FALSE FALSE FALSE

And it is said that you should use ifelse in such a case. Would you debug it?

mgclaros avatar Dec 09 '22 11:12 mgclaros

Installing the development version solves the error like

remotes::install_github("jkeirstead/scholar")

bappa10085 avatar Apr 03 '23 07:04 bappa10085

Full local function to fix this error @(mgclaros)

predict_h_index <- function(id, journals = NULL) {
    id <- tidy_id(id)

    # Getting the h-index and checking for NA
    h <- get_profile(id)$h_index
    if (is.na(h)) return(NA)

    n <- get_num_articles(id) # number of articles written
    y <- as.numeric(format(Sys.Date(), "%Y")) - get_oldest_article(id)
    j <- get_num_distinct_journals(id)

    # Handle optional journals parameter
    if (is.null(journals)) {
        q <- get_num_top_journals(id)
    } else {
        q <- get_num_top_journals(id, journals)
    }

    # Regression coefficients
    coefs <- c(
        1, 0.760, 0.373, 0.967, -0.069, 0.018, 0.033,
        2, 1.413, 0.781, 0.936, -0.132, 0.018, 0.064,
        3, 2.227, 1.105, 0.903, -0.193, 0.027, 0.096,
        4, 3.196, 1.386, 0.871, -0.274, 0.039, 0.145,
        5, 3.997, 1.578, 0.858, -0.345, 0.063, 0.198,
        6, 4.752, 1.671, 0.817, -0.377, 0.117, 0.282,
        7, 5.741, 1.761, 0.761, -0.420, 0.170, 0.394,
        8, 6.531, 1.796, 0.669, -0.420, 0.252, 0.508,
        9, 7.482, 1.653, 0.561, -0.415, 0.383, 0.629,
        10, 8.734, 1.326, 0.478, -0.411, 0.522, 0.823
    )
    coefs.m <- matrix(coefs, nrow=10, byrow=TRUE)
    coefs <- coefs.m[,-1]
    vals <- c(1, sqrt(n), h, y, j, q)

    # Calculate the h-index predictions
    h.pred <- coefs %*% vals
    h.vals <- c(h, h.pred)

    # Check for sensible values
    standard.warning <- "You're probably not a neuroscientist. Please read the documentation for information on the limitations of this function."
  
    if (any(diff(h.vals) < 0)) {
        warning(paste0("Decreasing h-values predicted. ", standard.warning))
    }

    if (any(h.vals < 0)) {
        warning(paste0("Negative h-values predicted. ", standard.warning))
    }
  
    return(data.frame(years_ahead = 0:10, h_index = h.vals))
}

ian-adams avatar Dec 02 '23 21:12 ian-adams

The development version did not function, but the 'alternative' the function worked fine :-) Thanks!

mgclaros avatar Dec 04 '23 08:12 mgclaros