public RandomForest trim(int ntrees) {
        if (ntrees > models.length) {
            throw new IllegalArgumentException("The new model size is larger than the current size.");
        }
        
        if (ntrees <= 0) {
            throw new IllegalArgumentException("Invalid new model size: " + ntrees);
        }

        Arrays.sort(models, Comparator.comparingDouble(model -> model.metrics.rmse));
        return new RandomForest(formula, Arrays.copyOf(models, ntrees), metrics, importance);
    }
@Test
    public void testTrim() {
        System.out.println("trim");

        RandomForest model = RandomForest.fit(Abalone.formula, Abalone.train, 50, 3, 20, 100, 5, 1.0, Arrays.stream(seeds));
        System.out.println(model.metrics());
        assertEquals(50, model.size());

        double rmse = RMSE.of(Abalone.testy, model.predict(Abalone.test));
        System.out.format("RMSE = %.4f%n", rmse);
        assertEquals(2.0858, rmse, 1E-4);

        RandomForest trimmed = model.trim(40);
        assertEquals(50, model.size());
        assertEquals(40, trimmed.size());

        double rmse1 = Arrays.stream(model.models()).mapToDouble(m -> m.metrics.rmse).max().getAsDouble();
        double rmse2 = Arrays.stream(trimmed.models()).mapToDouble(m -> m.metrics.rmse).max().getAsDouble();
        assertTrue(rmse1 > rmse2);

        rmse = RMSE.of(Abalone.testy, trimmed.predict(Abalone.test));
        assertEquals(2.0897, rmse, 1E-4);
    }