The R Project SVN R

Rev

Rev 7002 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 7002 Rev 7192
Line 1477... Line 1477...
1477
    SEXP expr, factors, terms, v, vars, vnames, assign;
1477
    SEXP expr, factors, terms, v, vars, vnames, assign;
1478
    SEXP xnames, tnames, rnames;
1478
    SEXP xnames, tnames, rnames;
1479
    SEXP count, contrast, contr1, contr2, nlevs, ordered, columns, x;
1479
    SEXP count, contrast, contr1, contr2, nlevs, ordered, columns, x;
1480
    SEXP variable, var_i;
1480
    SEXP variable, var_i;
1481
    int fik, first, i, j, k, kk, ll, n, nc, nterms, nvar;
1481
    int fik, first, i, j, k, kk, ll, n, nc, nterms, nvar;
1482
    int intercept, jstart, jnext, response, index;
1482
    int intercept, jstart, jnext, response, index, rhs_response;
1483
    char buf[BUFSIZE], *bufp;
1483
    char buf[BUFSIZE], *bufp, *addp;
1484
 
1484
 
1485
    checkArity(op, args);
1485
    checkArity(op, args);
1486
 
1486
 
1487
    /* Get the "terms" structure and extract */
1487
    /* Get the "terms" structure and extract */
1488
    /* the intercept and response attributes. */
1488
    /* the intercept and response attributes. */
Line 1551... Line 1551...
1551
    v = vars;
1551
    v = vars;
1552
    for (i = 0; i < nvar; i++) {
1552
    for (i = 0; i < nvar; i++) {
1553
	var_i = VECTOR(variable)[i] = VECTOR(vars)[i];
1553
	var_i = VECTOR(variable)[i] = VECTOR(vars)[i];
1554
	if (nrows(var_i) != n)
1554
	if (nrows(var_i) != n)
1555
	    errorcall(call, "variable lengths differ");
1555
	    errorcall(call, "variable lengths differ");
1556
	if (i == response - 1) {
1556
	/*if (i == response - 1) {
1557
	    LOGICAL(ordered)[0] = 0;
1557
	    LOGICAL(ordered)[0] = 0;
1558
	    INTEGER(nlevs)[0] = 0;
1558
	    INTEGER(nlevs)[0] = 0;
1559
	    INTEGER(columns)[0] = 0;
1559
	    INTEGER(columns)[0] = 0;
1560
	}
1560
	}
-
 
1561
	else */
1561
	else if (isOrdered(var_i)) {
1562
	if (isOrdered(var_i)) {
1562
	    LOGICAL(ordered)[i] = 1;
1563
	    LOGICAL(ordered)[i] = 1;
1563
	    INTEGER(nlevs)[i] = nlevels(var_i);
1564
	    INTEGER(nlevs)[i] = nlevels(var_i);
1564
	    INTEGER(columns)[i] = ncols(var_i);
1565
	    INTEGER(columns)[i] = ncols(var_i);
1565
	}
1566
	}
1566
	else if (isUnordered(var_i)) {
1567
	else if (isUnordered(var_i)) {
Line 1635... Line 1636...
1635
		VECTOR(contr2)[i] = eval(expr, rho);
1636
		VECTOR(contr2)[i] = eval(expr, rho);
1636
	    }
1637
	    }
1637
	}
1638
	}
1638
    }
1639
    }
1639
 
1640
 
-
 
1641
    /* By convention, an rhs term identical to the response generates nothing
-
 
1642
       in the model matrix (but interactions involving the response do). */
-
 
1643
 
-
 
1644
    rhs_response = -1;
-
 
1645
    if (response > 0) /* there is a response specified */
-
 
1646
	for (j = 0; j < nterms; j++)
-
 
1647
	    if (INTEGER(factors)[response - 1 + j * nvar]) {
-
 
1648
		for (i = 0, k = 0; i < nvar; i++)
-
 
1649
		    k += INTEGER(factors)[i + j * nvar] > 0;
-
 
1650
		if (k == 1) {
-
 
1651
		    rhs_response = j;
-
 
1652
		    break;
-
 
1653
		}
-
 
1654
	    }
-
 
1655
    
-
 
1656
 
1640
    /* We now have everything needed to build the design matrix. */
1657
    /* We now have everything needed to build the design matrix. */
1641
    /* The first step is to compute the matrix size and to allocate it. */
1658
    /* The first step is to compute the matrix size and to allocate it. */
1642
    /* Note that "count" holds a count of how many columns there are */
1659
    /* Note that "count" holds a count of how many columns there are */
1643
    /* for each term in the model and "nc" gives the total column count. */
1660
    /* for each term in the model and "nc" gives the total column count. */
1644
 
1661
 
Line 1646... Line 1663...
1646
    if (intercept)
1663
    if (intercept)
1647
	nc = 1;
1664
	nc = 1;
1648
    else
1665
    else
1649
	nc = 0;
1666
	nc = 0;
1650
    for (j = 0; j < nterms; j++) {
1667
    for (j = 0; j < nterms; j++) {
-
 
1668
	if (j == rhs_response) continue;
1651
	k = 1;
1669
	k = 1;
1652
	for (i = 0; i < nvar; i++) {
1670
	for (i = 0; i < nvar; i++) {
1653
	    if (INTEGER(factors)[i + j * nvar]) {
1671
	    if (INTEGER(factors)[i + j * nvar]) {
1654
		if (INTEGER(nlevs)[i]) {
1672
		if (INTEGER(nlevs)[i]) {
1655
		    switch(INTEGER(factors)[i + j * nvar]) {
1673
		    switch(INTEGER(factors)[i + j * nvar]) {
Line 1697... Line 1715...
1697
    /* matrix, assembling the names. */
1715
    /* matrix, assembling the names. */
1698
 
1716
 
1699
    /* FIXME : The body within these two loops should be embedded */
1717
    /* FIXME : The body within these two loops should be embedded */
1700
    /* in its own function. */
1718
    /* in its own function. */
1701
 
1719
 
1702
    /* FIXME : we need to check for buffer overflow here. */
-
 
1703
 
-
 
1704
    k = 0;
1720
    k = 0;
1705
    if (intercept)
1721
    if (intercept)
1706
	STRING(xnames)[k++] = mkChar("(Intercept)");
1722
	STRING(xnames)[k++] = mkChar("(Intercept)");
1707
 
1723
 
1708
    for (j = 0; j < nterms; j++) {
1724
    for (j = 0; j < nterms; j++) {
-
 
1725
	if (j == rhs_response) continue;
1709
	for (kk = 0; kk < INTEGER(count)[j]; kk++) {
1726
	for (kk = 0; kk < INTEGER(count)[j]; kk++) {
1710
	    first = 1;
1727
	    first = 1;
1711
	    index = kk;
1728
	    index = kk;
1712
	    bufp = &buf[0];
1729
	    bufp = &buf[0];
1713
	    for (i = 0; i < nvar; i++) {
1730
	    for (i = 0; i < nvar; i++) {
Line 1724... Line 1741...
1724
			}
1741
			}
1725
			else {
1742
			else {
1726
			    x = ColumnNames(VECTOR(contr2)[i]);
1743
			    x = ColumnNames(VECTOR(contr2)[i]);
1727
			    ll = ncols(VECTOR(contr2)[i]);
1744
			    ll = ncols(VECTOR(contr2)[i]);
1728
			}
1745
			}
1729
			bufp = AppendString(bufp, CHAR(STRING(vnames)[i]));
1746
			addp = CHAR(STRING(vnames)[i]);
-
 
1747
			if(strlen(buf) + strlen(addp) < BUFSIZE)
-
 
1748
			    bufp = AppendString(bufp, addp);
-
 
1749
			else 
-
 
1750
			    warningcall(call, "term names will be truncated");
1730
			if (x == R_NilValue)
1751
			if (x == R_NilValue) {
-
 
1752
			    if(strlen(buf) + 10 < BUFSIZE)
1731
			    bufp = AppendInteger(bufp, index % ll + 1);
1753
				bufp = AppendInteger(bufp, index % ll + 1);
-
 
1754
			    else 
-
 
1755
				warningcall(call, "term names will be truncated");
1732
			else
1756
			} else {
-
 
1757
			    addp = CHAR(STRING(x)[index % ll]);
-
 
1758
			    if(strlen(buf) + strlen(addp) < BUFSIZE)
1733
			    bufp = AppendString(bufp,
1759
				bufp = AppendString(bufp, addp);
-
 
1760
			    else 
1734
					 CHAR(STRING(x)[index % ll]));
1761
				warningcall(call, "term names will be truncated");
-
 
1762
			}
1735
		    }
1763
		    }
1736
		    else {
1764
		    else {
1737
			x = ColumnNames(var_i);
1765
			x = ColumnNames(var_i);
1738
			ll = ncols(var_i);
1766
			ll = ncols(var_i);
1739
			bufp = AppendString(bufp, CHAR(STRING(vnames)[i]));
1767
			addp = CHAR(STRING(vnames)[i]);
-
 
1768
			if(strlen(buf) + strlen(addp) < BUFSIZE)
-
 
1769
			    bufp = AppendString(bufp, addp);
-
 
1770
			else 
-
 
1771
			    warningcall(call, "term names will be truncated");
1740
			if (ll > 1) {
1772
			if (ll > 1) {
1741
			    if (x == R_NilValue)
1773
			    if (x == R_NilValue) {
-
 
1774
				if(strlen(buf) + 10 < BUFSIZE)
1742
				bufp = AppendInteger(bufp, index % ll + 1);
1775
				    bufp = AppendInteger(bufp, index % ll + 1);
-
 
1776
				else 
-
 
1777
				    warningcall(call, "term names will be truncated");		
1743
			    else
1778
			    } else {
-
 
1779
				addp = CHAR(STRING(x)[index % ll]);
-
 
1780
				if(strlen(buf) + strlen(addp) < BUFSIZE)
1744
				bufp = AppendString(bufp,
1781
				    bufp = AppendString(bufp, addp);
-
 
1782
				else 
1745
					     CHAR(STRING(x)[index % ll]));
1783
				    warningcall(call, "term names will be truncated");
-
 
1784
			    }
1746
			}
1785
			}
1747
		    }
1786
		    }
1748
		    index = index / ll;
1787
		    index = index / ll;
1749
		}
1788
		}
1750
	    }
1789
	    }
Line 1766... Line 1805...
1766
 
1805
 
1767
    /* b) Now loop over the model terms */
1806
    /* b) Now loop over the model terms */
1768
 
1807
 
1769
    contrast = R_NilValue;	/* -Wall */
1808
    contrast = R_NilValue;	/* -Wall */
1770
    for (k = 0; k < nterms; k++) {
1809
    for (k = 0; k < nterms; k++) {
-
 
1810
	if (k == rhs_response) continue;
1771
	for (i = 0; i < nvar; i++) {
1811
	for (i = 0; i < nvar; i++) {
1772
	    if (INTEGER(columns)[i] == 0)
1812
	    if (INTEGER(columns)[i] == 0)
1773
		continue;
1813
		continue;
1774
	    var_i = VECTOR(variable)[i];
1814
	    var_i = VECTOR(variable)[i];
1775
	    fik = INTEGER(factors)[i + k * nvar];
1815
	    fik = INTEGER(factors)[i + k * nvar];